diff --git a/generate.go b/generate.go index 21ce370a..cc3bc5e2 100644 --- a/generate.go +++ b/generate.go @@ -116,5 +116,5 @@ func main() { generateFile("js", "static", "Javascript", "server/static/js/*.js", "server/static/js.go") generateFile("none", "template", "templateViewsMap", "server/template/html/*.html", "server/template/views.go") generateFile("none", "template", "templateCommonMap", "server/template/html/common/*.html", "server/template/common.go") - generateFile("none", "locale", "Translations", "locale/translations/*.json", "locale/translations.go") + generateFile("none", "locale", "translations", "locale/translations/*.json", "locale/translations.go") } diff --git a/locale/language.go b/locale/language.go index c3deda33..b63976ca 100644 --- a/locale/language.go +++ b/locale/language.go @@ -6,11 +6,13 @@ package locale import "fmt" +// Language represents a language in the system. type Language struct { language string translations Translation } +// Get fetch the translation for the given key. func (l *Language) Get(key string, args ...interface{}) string { var translation string @@ -24,11 +26,12 @@ func (l *Language) Get(key string, args ...interface{}) string { return fmt.Sprintf(translation, args...) } +// Plural returns the translation of the given key by using the language plural form. func (l *Language) Plural(key string, n int, args ...interface{}) string { translation := key slices, found := l.translations[key] - if found { + if found { pluralForm, found := pluralForms[l.language] if !found { pluralForm = pluralForms["default"] diff --git a/locale/locale.go b/locale/locale.go index 49005259..257ec2be 100644 --- a/locale/locale.go +++ b/locale/locale.go @@ -6,16 +6,19 @@ package locale import "log" +// Translation is the translation mapping table. type Translation map[string]interface{} +// Locales represents locales supported by the system. type Locales map[string]Translation +// Load prepare the locale system by loading all translations. func Load() *Translator { translator := NewTranslator() - for language, translations := range Translations { + for language, tr := range translations { log.Println("Loading translation:", language) - translator.AddLanguage(language, translations) + translator.AddLanguage(language, tr) } return translator diff --git a/locale/translations.go b/locale/translations.go index 863c7469..766f8add 100644 --- a/locale/translations.go +++ b/locale/translations.go @@ -1,9 +1,9 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-25 10:47:30.371865884 -0800 PST m=+0.027993990 +// 2017-11-25 15:50:52.572283626 -0800 PST m=+0.030941705 package locale -var Translations = map[string]string{ +var translations = map[string]string{ "en_US": `{ "plural.feed.error_count": [ "%d error", @@ -149,12 +149,13 @@ var Translations = map[string]string{ "Sign in with Google": "Se connecter avec Google", "Unlink my Google account": "Dissocier mon compte Google", "Link my Google account": "Associer mon compte Google", - "Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur." + "Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur.", + "Invalid theme.": "Le thème est invalide." } `, } -var TranslationsChecksums = map[string]string{ +var translationsChecksums = map[string]string{ "en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897", - "fr_FR": "92b360ebbfae5c243897c05f7869e6bef0129a2c8275b6802fbaba0d05410e91", + "fr_FR": "48622d4796fe4a461221565d84f52e22fb167a44a870b08ba32887897bdfbb1a", } diff --git a/locale/translator.go b/locale/translator.go index 5560dd6c..18206b07 100644 --- a/locale/translator.go +++ b/locale/translator.go @@ -10,10 +10,12 @@ import ( "strings" ) +// Translator manage supported locales. type Translator struct { - Locales Locales + locales Locales } +// AddLanguage loads a new language into the system. func (t *Translator) AddLanguage(language, translations string) error { var decodedTranslations Translation @@ -22,12 +24,13 @@ func (t *Translator) AddLanguage(language, translations string) error { return fmt.Errorf("Invalid JSON file: %v", err) } - t.Locales[language] = decodedTranslations + t.locales[language] = decodedTranslations return nil } +// GetLanguage returns the given language handler. func (t *Translator) GetLanguage(language string) *Language { - translations, found := t.Locales[language] + translations, found := t.locales[language] if !found { return &Language{language: language} } @@ -35,6 +38,7 @@ func (t *Translator) GetLanguage(language string) *Language { return &Language{language: language, translations: translations} } +// NewTranslator creates a new Translator. func NewTranslator() *Translator { - return &Translator{Locales: make(Locales)} + return &Translator{locales: make(Locales)} }