summaryrefslogtreecommitdiffstats
path: root/modules/translation/translation.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/translation/translation.go')
-rw-r--r--modules/translation/translation.go38
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