3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

Split language into a subpackage

This commit is contained in:
Daniel Oaks 2018-02-03 19:46:14 +10:00
parent 1b14230758
commit afd6ca610c
5 changed files with 32 additions and 29 deletions

View File

@ -19,6 +19,7 @@ import (
"code.cloudfoundry.org/bytefmt" "code.cloudfoundry.org/bytefmt"
"github.com/oragono/oragono/irc/connection_limits" "github.com/oragono/oragono/irc/connection_limits"
"github.com/oragono/oragono/irc/custime" "github.com/oragono/oragono/irc/custime"
"github.com/oragono/oragono/irc/languages"
"github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/logger"
"github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/passwd"
"github.com/oragono/oragono/irc/utils" "github.com/oragono/oragono/irc/utils"
@ -145,15 +146,6 @@ type StackImpactConfig struct {
AppName string `yaml:"app-name"` 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. // Config defines the overall configuration.
type Config struct { type Config struct {
Network struct { Network struct {
@ -182,7 +174,7 @@ type Config struct {
Enabled bool Enabled bool
Path string Path string
Default string Default string
Data map[string]LangData Data map[string]languages.LangData
} }
Datastore struct { Datastore struct {
@ -484,7 +476,7 @@ func LoadConfig(filename string) (config *Config, err error) {
} }
// get language files // get language files
config.Languages.Data = make(map[string]LangData) config.Languages.Data = make(map[string]languages.LangData)
if config.Languages.Enabled { if config.Languages.Enabled {
files, err := ioutil.ReadDir(config.Languages.Path) files, err := ioutil.ReadDir(config.Languages.Path)
if err != nil { 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()) 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) err = yaml.Unmarshal(data, &langInfo)
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not parse language file [%s]: %s", name, err.Error()) return nil, fmt.Errorf("Could not parse language file [%s]: %s", name, err.Error())

View File

@ -11,7 +11,6 @@ import (
"net" "net"
"github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/passwd"
"github.com/oragono/oragono/irc/utils" "github.com/oragono/oragono/irc/utils"
) )

View File

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"github.com/oragono/oragono/irc/languages"
) )
// HelpEntryType represents the different sorts of help entries that can exist. // 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 var HelpIndexOpers map[string]string
// GenerateHelpIndex is used to generate HelpIndex. // 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 // generate the help entry lists
var commands, isupport, information []string var commands, isupport, information []string
@ -638,7 +640,7 @@ Information:
} }
// GenerateHelpIndices generates our help indexes and confirms we have HELP entries for every command. // 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 // startup check that we have HELP entries for every command
if len(HelpIndex) == 0 && len(HelpIndexOpers) == 0 { if len(HelpIndex) == 0 && len(HelpIndexOpers) == 0 {
for name := range Commands { for name := range Commands {

View File

@ -1,7 +1,7 @@
// Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net> // Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license // released under the MIT license
package irc package languages
import ( import (
"fmt" "fmt"
@ -10,17 +10,26 @@ import (
"sync" "sync"
) )
// LanguageManager manages our languages and provides translation abilities. // LangData is the data contained in a language file.
type LanguageManager struct { 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 sync.RWMutex
Info map[string]LangData Info map[string]LangData
translations map[string]map[string]string translations map[string]map[string]string
defaultLang string defaultLang string
} }
// NewLanguageManager returns a new LanguageManager. // NewManager returns a new Manager.
func NewLanguageManager(defaultLang string, languageData map[string]LangData) *LanguageManager { func NewManager(defaultLang string, languageData map[string]LangData) *Manager {
lm := LanguageManager{ lm := Manager{
Info: make(map[string]LangData), Info: make(map[string]LangData),
translations: make(map[string]map[string]string), translations: make(map[string]map[string]string),
defaultLang: defaultLang, defaultLang: defaultLang,
@ -51,7 +60,7 @@ func NewLanguageManager(defaultLang string, languageData map[string]LangData) *L
} }
// Default returns the default languages. // Default returns the default languages.
func (lm *LanguageManager) Default() []string { func (lm *Manager) Default() []string {
lm.RLock() lm.RLock()
defer lm.RUnlock() defer lm.RUnlock()
@ -62,7 +71,7 @@ func (lm *LanguageManager) Default() []string {
} }
// Count returns how many languages we have. // Count returns how many languages we have.
func (lm *LanguageManager) Count() int { func (lm *Manager) Count() int {
lm.RLock() lm.RLock()
defer lm.RUnlock() defer lm.RUnlock()
@ -70,7 +79,7 @@ func (lm *LanguageManager) Count() int {
} }
// Translators returns the languages we have and the translators. // Translators returns the languages we have and the translators.
func (lm *LanguageManager) Translators() []string { func (lm *Manager) Translators() []string {
lm.RLock() lm.RLock()
defer lm.RUnlock() defer lm.RUnlock()
@ -88,7 +97,7 @@ func (lm *LanguageManager) Translators() []string {
} }
// Codes returns the proper language codes for the given casefolded language codes. // 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() lm.RLock()
defer lm.RUnlock() defer lm.RUnlock()
@ -108,7 +117,7 @@ func (lm *LanguageManager) Codes(codes []string) []string {
} }
// Translate returns the given string, translated into the given language. // 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 // not using any special languages
if len(languages) == 0 || languages[0] == "en" || len(lm.translations) == 0 { if len(languages) == 0 || languages[0] == "en" || len(lm.translations) == 0 {
return originalString return originalString

View File

@ -27,6 +27,7 @@ import (
"github.com/oragono/oragono/irc/caps" "github.com/oragono/oragono/irc/caps"
"github.com/oragono/oragono/irc/connection_limits" "github.com/oragono/oragono/irc/connection_limits"
"github.com/oragono/oragono/irc/isupport" "github.com/oragono/oragono/irc/isupport"
"github.com/oragono/oragono/irc/languages"
"github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/logger"
"github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/passwd"
"github.com/oragono/oragono/irc/sno" "github.com/oragono/oragono/irc/sno"
@ -93,7 +94,7 @@ type Server struct {
loggingRawIO bool loggingRawIO bool
isupport *isupport.List isupport *isupport.List
klines *KLineManager klines *KLineManager
languages *LanguageManager languages *languages.Manager
limits Limits limits Limits
listeners map[string]*ListenerWrapper listeners map[string]*ListenerWrapper
logger *logger.Manager logger *logger.Manager
@ -142,7 +143,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
clients: NewClientManager(), clients: NewClientManager(),
connectionLimiter: connection_limits.NewLimiter(), connectionLimiter: connection_limits.NewLimiter(),
connectionThrottler: connection_limits.NewThrottler(), 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), listeners: make(map[string]*ListenerWrapper),
logger: logger, logger: logger,
monitorManager: NewMonitorManager(), monitorManager: NewMonitorManager(),
@ -775,7 +776,7 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
CapValues.Set(caps.Languages, newLanguageValue) 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") server.logger.Debug("rehash", "Regenerating HELP indexes for new languages")
GenerateHelpIndices(lm) GenerateHelpIndices(lm)