From 0a1ab455ff8d685b4fc1140f1fc001ad7674d4bb Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Tue, 30 Jul 2024 21:56:56 +0100 Subject: [PATCH] Fall back to English if there is no sensible match Fixes #5040 --- lang/lang.go | 1 + lang/lang_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lang/lang.go b/lang/lang.go index 3f0b6b867b..50f15cdbc1 100644 --- a/lang/lang.go +++ b/lang/lang.go @@ -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) diff --git a/lang/lang_test.go b/lang/lang_test.go index 6d090201ec..2726230e15 100644 --- a/lang/lang_test.go +++ b/lang/lang_test.go @@ -4,6 +4,7 @@ import ( "testing" "fyne.io/fyne/v2" + "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/stretchr/testify/assert" ) @@ -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")) }