Skip to content

Commit bb1e0d0

Browse files
wxiaoguanglafriks
andauthored
Use en-US as fallback when using other default language (#21200)
Only en-US has complete translations. When use other language as default, the en-US should still be used as fallback. Close #21199 ### Screenshot ![image](https://user-images.githubusercontent.com/2114189/190882906-b7a83958-0ea2-46c4-9084-42c4f9a239aa.png) Co-authored-by: Lauris BH <[email protected]>
1 parent 0c8ce71 commit bb1e0d0

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
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/i18n_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func Test_Tr(t *testing.T) {
13+
func TestLocaleStore(t *testing.T) {
1414
testData1 := []byte(`
1515
.dot.name = Dot Name
1616
fmt = %[1]s %[2]s
@@ -28,8 +28,8 @@ sub = Changed Sub String
2828
`)
2929

3030
ls := NewLocaleStore()
31-
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1))
32-
assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2))
31+
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, nil))
32+
assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2, nil))
3333
ls.SetDefaultLang("lang1")
3434

3535
result := ls.Tr("lang1", "fmt", "a", "b")
@@ -58,3 +58,21 @@ sub = Changed Sub String
5858
assert.False(t, found)
5959
assert.NoError(t, ls.Close())
6060
}
61+
62+
func TestLocaleStoreMoreSource(t *testing.T) {
63+
testData1 := []byte(`
64+
a=11
65+
b=12
66+
`)
67+
68+
testData2 := []byte(`
69+
b=21
70+
c=22
71+
`)
72+
73+
ls := NewLocaleStore()
74+
assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, testData2))
75+
assert.Equal(t, "11", ls.Tr("lang1", "a"))
76+
assert.Equal(t, "21", ls.Tr("lang1", "b"))
77+
assert.Equal(t, "22", ls.Tr("lang1", "c"))
78+
}

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)