Skip to content

Commit

Permalink
Fall back to English if there is no sensible match
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jul 30, 2024
1 parent 49ad989 commit 0a1ab45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func init() {
bundle = i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)

translated = []language.Tag{language.Make("en")} // the first item in this list will be the fallback if none match
err := AddTranslationsFS(translations, "translations")
if err != nil {
fyne.LogError("Error occurred loading built-in translations", err)
Expand Down
24 changes: 24 additions & 0 deletions lang/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"fyne.io/fyne/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,6 +24,29 @@ func TestAddTranslations(t *testing.T) {
assert.Equal(t, "Match2", L("Test2"))
}

func TestLocalize_Default(t *testing.T) {
setupLang("en")
err := AddTranslationsForLocale([]byte(`{
"Test": "Match"
}`), "en")
assert.Nil(t, err)

err = AddTranslationsForLocale([]byte(`{
"Test": "Match2"
}`), "de")
assert.Nil(t, err)

fr := i18n.NewLocalizer(bundle, "fr")
ret, err := fr.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Test",
},
})

assert.NotNil(t, err) // we are falling back
assert.Equal(t, "Match", ret)
}

func TestLocalize_Fallback(t *testing.T) {
assert.Equal(t, "Missing", L("Missing"))
}
Expand Down

0 comments on commit 0a1ab45

Please sign in to comment.