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"
"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())

View File

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

View File

@ -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 {

View File

@ -1,7 +1,7 @@
// Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
// 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

View File

@ -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)