From afd6ca610c21631fde54d9e043e2ff5dc6f5327d Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Sat, 3 Feb 2018 19:46:14 +1000 Subject: [PATCH] Split language into a subpackage --- irc/config.go | 16 ++++------------ irc/gateways.go | 1 - irc/help.go | 6 ++++-- irc/{ => languages}/languages.go | 31 ++++++++++++++++++++----------- irc/server.go | 7 ++++--- 5 files changed, 32 insertions(+), 29 deletions(-) rename irc/{ => languages}/languages.go (78%) diff --git a/irc/config.go b/irc/config.go index 9d80736a..256244d7 100644 --- a/irc/config.go +++ b/irc/config.go @@ -19,6 +19,7 @@ import ( "code.cloudfoundry.org/bytefmt" "github.com/oragono/oragono/irc/connection_limits" "github.com/oragono/oragono/irc/custime" + "github.com/oragono/oragono/irc/languages" "github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/utils" @@ -145,15 +146,6 @@ type StackImpactConfig struct { AppName string `yaml:"app-name"` } -// LangData is the data contained in a language file. -type LangData struct { - Name string - Code string - Contributors string - Incomplete bool - Translations map[string]string -} - // Config defines the overall configuration. type Config struct { Network struct { @@ -182,7 +174,7 @@ type Config struct { Enabled bool Path string Default string - Data map[string]LangData + Data map[string]languages.LangData } Datastore struct { @@ -484,7 +476,7 @@ func LoadConfig(filename string) (config *Config, err error) { } // get language files - config.Languages.Data = make(map[string]LangData) + config.Languages.Data = make(map[string]languages.LangData) if config.Languages.Enabled { files, err := ioutil.ReadDir(config.Languages.Path) if err != nil { @@ -514,7 +506,7 @@ func LoadConfig(filename string) (config *Config, err error) { return nil, fmt.Errorf("Could not load language file [%s]: %s", name, err.Error()) } - var langInfo LangData + var langInfo languages.LangData err = yaml.Unmarshal(data, &langInfo) if err != nil { return nil, fmt.Errorf("Could not parse language file [%s]: %s", name, err.Error()) diff --git a/irc/gateways.go b/irc/gateways.go index 54e6e3ef..ffaee157 100644 --- a/irc/gateways.go +++ b/irc/gateways.go @@ -11,7 +11,6 @@ import ( "net" "github.com/oragono/oragono/irc/passwd" - "github.com/oragono/oragono/irc/utils" ) diff --git a/irc/help.go b/irc/help.go index 6bd3f5a6..86fe7a8a 100644 --- a/irc/help.go +++ b/irc/help.go @@ -7,6 +7,8 @@ import ( "fmt" "sort" "strings" + + "github.com/oragono/oragono/irc/languages" ) // HelpEntryType represents the different sorts of help entries that can exist. @@ -576,7 +578,7 @@ var HelpIndex map[string]string var HelpIndexOpers map[string]string // GenerateHelpIndex is used to generate HelpIndex. -func GenerateHelpIndex(lm *LanguageManager, forOpers bool) map[string]string { +func GenerateHelpIndex(lm *languages.Manager, forOpers bool) map[string]string { // generate the help entry lists var commands, isupport, information []string @@ -638,7 +640,7 @@ Information: } // GenerateHelpIndices generates our help indexes and confirms we have HELP entries for every command. -func GenerateHelpIndices(lm *LanguageManager) error { +func GenerateHelpIndices(lm *languages.Manager) error { // startup check that we have HELP entries for every command if len(HelpIndex) == 0 && len(HelpIndexOpers) == 0 { for name := range Commands { diff --git a/irc/languages.go b/irc/languages/languages.go similarity index 78% rename from irc/languages.go rename to irc/languages/languages.go index 399c9c46..39d43e73 100644 --- a/irc/languages.go +++ b/irc/languages/languages.go @@ -1,7 +1,7 @@ // Copyright (c) 2018 Daniel Oaks // released under the MIT license -package irc +package languages import ( "fmt" @@ -10,17 +10,26 @@ import ( "sync" ) -// LanguageManager manages our languages and provides translation abilities. -type LanguageManager struct { +// LangData is the data contained in a language file. +type LangData struct { + Name string + Code string + Contributors string + Incomplete bool + Translations map[string]string +} + +// Manager manages our languages and provides translation abilities. +type Manager struct { sync.RWMutex Info map[string]LangData translations map[string]map[string]string defaultLang string } -// NewLanguageManager returns a new LanguageManager. -func NewLanguageManager(defaultLang string, languageData map[string]LangData) *LanguageManager { - lm := LanguageManager{ +// NewManager returns a new Manager. +func NewManager(defaultLang string, languageData map[string]LangData) *Manager { + lm := Manager{ Info: make(map[string]LangData), translations: make(map[string]map[string]string), defaultLang: defaultLang, @@ -51,7 +60,7 @@ func NewLanguageManager(defaultLang string, languageData map[string]LangData) *L } // Default returns the default languages. -func (lm *LanguageManager) Default() []string { +func (lm *Manager) Default() []string { lm.RLock() defer lm.RUnlock() @@ -62,7 +71,7 @@ func (lm *LanguageManager) Default() []string { } // Count returns how many languages we have. -func (lm *LanguageManager) Count() int { +func (lm *Manager) Count() int { lm.RLock() defer lm.RUnlock() @@ -70,7 +79,7 @@ func (lm *LanguageManager) Count() int { } // Translators returns the languages we have and the translators. -func (lm *LanguageManager) Translators() []string { +func (lm *Manager) Translators() []string { lm.RLock() defer lm.RUnlock() @@ -88,7 +97,7 @@ func (lm *LanguageManager) Translators() []string { } // Codes returns the proper language codes for the given casefolded language codes. -func (lm *LanguageManager) Codes(codes []string) []string { +func (lm *Manager) Codes(codes []string) []string { lm.RLock() defer lm.RUnlock() @@ -108,7 +117,7 @@ func (lm *LanguageManager) Codes(codes []string) []string { } // Translate returns the given string, translated into the given language. -func (lm *LanguageManager) Translate(languages []string, originalString string) string { +func (lm *Manager) Translate(languages []string, originalString string) string { // not using any special languages if len(languages) == 0 || languages[0] == "en" || len(lm.translations) == 0 { return originalString diff --git a/irc/server.go b/irc/server.go index 22957fc5..32c881d2 100644 --- a/irc/server.go +++ b/irc/server.go @@ -27,6 +27,7 @@ import ( "github.com/oragono/oragono/irc/caps" "github.com/oragono/oragono/irc/connection_limits" "github.com/oragono/oragono/irc/isupport" + "github.com/oragono/oragono/irc/languages" "github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/sno" @@ -93,7 +94,7 @@ type Server struct { loggingRawIO bool isupport *isupport.List klines *KLineManager - languages *LanguageManager + languages *languages.Manager limits Limits listeners map[string]*ListenerWrapper logger *logger.Manager @@ -142,7 +143,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) { clients: NewClientManager(), connectionLimiter: connection_limits.NewLimiter(), connectionThrottler: connection_limits.NewThrottler(), - languages: NewLanguageManager(config.Languages.Default, config.Languages.Data), + languages: languages.NewManager(config.Languages.Default, config.Languages.Data), listeners: make(map[string]*ListenerWrapper), logger: logger, monitorManager: NewMonitorManager(), @@ -775,7 +776,7 @@ func (server *Server) applyConfig(config *Config, initial bool) error { CapValues.Set(caps.Languages, newLanguageValue) } - lm := NewLanguageManager(config.Languages.Default, config.Languages.Data) + lm := languages.NewManager(config.Languages.Default, config.Languages.Data) server.logger.Debug("rehash", "Regenerating HELP indexes for new languages") GenerateHelpIndices(lm)