diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-02-08 11:02:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 11:02:30 +0800 |
commit | a60e8be8d15e90a44f2a746a4e8d81a81e03d2db (patch) | |
tree | 256934930648471f3eddb1e4dcf2f077aaf95e0c /modules/translation | |
parent | 7b25a010c89835e036f1b0cc5af65249bfd2781b (diff) | |
download | gitea-a60e8be8d15e90a44f2a746a4e8d81a81e03d2db.tar.gz gitea-a60e8be8d15e90a44f2a746a4e8d81a81e03d2db.zip |
Refactor i18n, use Locale to provide i18n/translation related functions (#18648)
* remove unnecessary web context data fields, and unify the i18n/translation related functions to `Locale`
* in development, show an error if a translation key is missing
* remove the unnecessary loops `for _, lang := range translation.AllLangs()` for every request, which improves the performance slightly
* use `ctx.Locale.Language()` instead of `ctx.Data["Lang"].(string)`
* add more comments about how the Locale/LangType fields are used
Diffstat (limited to 'modules/translation')
-rw-r--r-- | modules/translation/translation.go | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/modules/translation/translation.go b/modules/translation/translation.go index 977f2cdc23..fd38e4d510 100644 --- a/modules/translation/translation.go +++ b/modules/translation/translation.go @@ -25,17 +25,18 @@ type Locale interface { // LangType represents a lang type type LangType struct { - Lang, Name string + Lang, Name string // these fields are used directly in templates: {{range .AllLangs}}{{.Lang}}{{.Name}}{{end}} } var ( matcher language.Matcher - allLangs []LangType + allLangs []*LangType + allLangMap map[string]*LangType supportedTags []language.Tag ) // AllLangs returns all supported languages sorted by name -func AllLangs() []LangType { +func AllLangs() []*LangType { return allLangs } @@ -81,14 +82,17 @@ func InitLocales() { } i18n.SetDefaultLang("en-US") - allLangs = make([]LangType, 0, i18n.Count()-1) + allLangs = make([]*LangType, 0, i18n.Count()) + allLangMap = map[string]*LangType{} langs := i18n.ListLangs() - names := i18n.ListLangDescs() + descs := i18n.ListLangDescs() for i, v := range langs { - allLangs = append(allLangs, LangType{v, names[i]}) + l := &LangType{v, descs[i]} + allLangs = append(allLangs, l) + allLangMap[v] = l } - // Sort languages case insensitive according to their name - needed for the user settings + // Sort languages case-insensitive according to their name - needed for the user settings sort.Slice(allLangs, func(i, j int) bool { return strings.ToLower(allLangs[i].Name) < strings.ToLower(allLangs[j].Name) }) @@ -102,13 +106,18 @@ func Match(tags ...language.Tag) language.Tag { // locale represents the information of localization. type locale struct { - Lang string + Lang, LangName string // these fields are used directly in templates: .i18n.Lang } // NewLocale return a locale func NewLocale(lang string) Locale { + langName := "unknown" + if l, ok := allLangMap[lang]; ok { + langName = l.Name + } return &locale{ - Lang: lang, + Lang: lang, + LangName: langName, } } @@ -118,7 +127,16 @@ func (l *locale) Language() string { // Tr translates content to target language. func (l *locale) Tr(format string, args ...interface{}) string { - return i18n.Tr(l.Lang, format, args...) + if setting.IsProd { + return i18n.Tr(l.Lang, format, args...) + } + + // in development, we should show an error if a translation key is missing + s, ok := TryTr(l.Lang, format, args...) + if !ok { + log.Error("missing i18n translation key: %q", format) + } + return s } // Language specific rules for translating plural texts |