Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to English if there is no sensible match #5043

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
dweymouth marked this conversation as resolved.
Show resolved Hide resolved
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) {
dweymouth marked this conversation as resolved.
Show resolved Hide resolved
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