Skip to content

Commit e6097d4

Browse files
committed
When use other language as default, the en-US should still be used as fallback.
1 parent 3219641 commit e6097d4

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

modules/translation/i18n/i18n.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type LocaleStore interface {
3434
// HasLang returns whether a given language is present in the store
3535
HasLang(langName string) bool
3636
// AddLocaleByIni adds a new language to the store
37-
AddLocaleByIni(langName, langDesc string, source interface{}) error
37+
AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error
3838
}
3939

4040
// ResetDefaultLocales resets the current default locales

modules/translation/i18n/localestore.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ func NewLocaleStore() LocaleStore {
3737
}
3838

3939
// AddLocaleByIni adds locale by ini into the store
40-
// if source is a string, then the file is loaded
41-
// if source is a []byte, then the content is used
42-
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source interface{}) error {
40+
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
4341
if _, ok := store.localeMap[langName]; ok {
4442
return ErrLocaleAlreadyExist
4543
}
@@ -53,7 +51,7 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, source inter
5351
iniFile, err := ini.LoadSources(ini.LoadOptions{
5452
IgnoreInlineComment: true,
5553
UnescapeValueCommentSymbols: true,
56-
}, source)
54+
}, source, moreSource)
5755
if err != nil {
5856
return fmt.Errorf("unable to load ini: %w", err)
5957
}

modules/translation/translation.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func InitLocales(ctx context.Context) {
6060
log.Fatal("Failed to list locale files: %v", err)
6161
}
6262

63-
localFiles := make(map[string]interface{}, len(localeNames))
63+
localeData := make(map[string][]byte, len(localeNames))
6464
for _, name := range localeNames {
65-
localFiles[name], err = options.Locale(name)
65+
localeData[name], err = options.Locale(name)
6666
if err != nil {
6767
log.Fatal("Failed to load %s locale file. %v", name, err)
6868
}
@@ -75,9 +75,17 @@ func InitLocales(ctx context.Context) {
7575

7676
matcher = language.NewMatcher(supportedTags)
7777
for i := range setting.Names {
78-
key := "locale_" + setting.Langs[i] + ".ini"
78+
var localeDataBase []byte
79+
if i == 0 && setting.Langs[0] != "en-US" {
80+
// Only en-US has complete translations. When use other language as default, the en-US should still be used as fallback.
81+
localeDataBase = localeData["locale_en-US.ini"]
82+
if localeDataBase == nil {
83+
log.Fatal("Failed to load locale_en-US.ini file.")
84+
}
85+
}
7986

80-
if err = i18n.DefaultLocales.AddLocaleByIni(setting.Langs[i], setting.Names[i], localFiles[key]); err != nil {
87+
key := "locale_" + setting.Langs[i] + ".ini"
88+
if err = i18n.DefaultLocales.AddLocaleByIni(setting.Langs[i], setting.Names[i], localeDataBase, localeData[key]); err != nil {
8189
log.Error("Failed to set messages to %s: %v", setting.Langs[i], err)
8290
}
8391
}

0 commit comments

Comments
 (0)