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

FEATURE: Setting preferred locale and fallback manually #5026

Draft
wants to merge 15 commits into
base: release/v2.5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
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
14 changes: 14 additions & 0 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
// More info available on the `LocalizePluralKey` function.
XN = LocalizePluralKey

preferredLanguage *fyne.Locale

bundle *i18n.Bundle
localizer *i18n.Localizer

Expand Down Expand Up @@ -159,6 +161,15 @@ func AddTranslationsFS(fs embed.FS, dir string) (retErr error) {
return retErr
}

// SetPreferredLanguage allows an app to set the preferred language for translations, overwriting the System Locale.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this adds a new public API, it will need to be targeted for Fyne 2.6 and should have a Since: 2.6 line in the comment (see other APIs for an example). Also the PR should be based against develop instead of the release branch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This builds on top of this bugfix #5016
which is i think not yet merged back into develop.

Once that is done, i am happy to base it against develop.
Or should i merge #5016 into develop myself?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave it to @andydotxyz to manage the branches since I'm not sure what our usual process is for keeping develop in sync with release/v2.x.x branches

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release branch goes back into develop after each point release, so it should have been done a few weeks back.

func SetPreferredLanguage(languageKey string) error {
tag, err := language.Parse(languageKey)
fyneLocale := fyne.Locale(tag.String())
preferredLanguage = &fyneLocale
updateLocalizer()
return err
}

func addLanguage(data []byte, name string) error {
f, err := bundle.ParseMessageFileBytes(data, name)
translated = append(translated, f.Tag)
Expand Down Expand Up @@ -198,6 +209,9 @@ func updateLocalizer() {
fyne.LogError("Failed to load user locales", err)
all = []string{"en"}
}
if preferredLanguage != nil {
all = append([]string{preferredLanguage.String()}, all...)
}
str := closestSupportedLocale(all).LanguageString()
setupLang(str)
localizer = i18n.NewLocalizer(bundle, str)
Expand Down
13 changes: 13 additions & 0 deletions lang/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ func TestLocalizePluralKey_Fallback(t *testing.T) {
assert.Equal(t, "Apple", XN("appleID", "Apple", 1))
assert.Equal(t, "Apples", XN("appleID", "Apple", 2))
}

func TestSetPreferredLanguage(t *testing.T) {
_ = AddTranslations(fyne.NewStaticResource("en.json", []byte(`{
"Test": "Match"
}`)))
_ = AddTranslations(fyne.NewStaticResource("fr.json", []byte(`{
"Test2": "Match2"
}`)))
setupLang("fr")
assert.Equal(t, "Match2", L("Test2"))
SetPreferredLanguage("en")
assert.Equal(t, "Match", L("Test"))
}