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 7 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
57 changes: 57 additions & 0 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"fyne.io/fyne/v2"

"golang.org/x/text/language"
"slices"

Check failure on line 19 in lang/lang.go

View workflow job for this annotation

GitHub Actions / windows_tests (1.20.x)

package slices is not in GOROOT (C:\hostedtoolcache\windows\go\1.20.14\x64\src\slices)

Check failure on line 19 in lang/lang.go

View workflow job for this annotation

GitHub Actions / mobile_tests (1.19.x)

package slices is not in GOROOT (/home/runner/work/fyne/setup-go-faster/go/1.19.13/x64/src/slices)

Check failure on line 19 in lang/lang.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.19.x, ubuntu-latest)

package slices is not in GOROOT (/home/runner/work/fyne/setup-go-faster/go/1.19.13/x64/src/slices)

Check failure on line 19 in lang/lang.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.19.x, macos-latest)

package slices is not in GOROOT (/Users/runner/work/fyne/setup-go-faster/go/1.19.13/x64/src/slices)
)

var (
Expand All @@ -35,6 +36,12 @@
// More info available on the `LocalizePluralKey` function.
XN = LocalizePluralKey

// This defines an order in which it will try to find a fallback in case localizer does not find a match.
// All other languages will be in order as the system reads them (which is most likely alphabetical).
languageOrder = []string{"en"}

preferredLanguage string

bundle *i18n.Bundle
localizer *i18n.Localizer

Expand Down Expand Up @@ -159,6 +166,22 @@
return retErr
}

// SetLanguageOrder allows an app to set the order in which translations are checked in case no locale matches.
// Since 2.6
func SetLanguageOrder(order []string) {
languageOrder = order
updateLocalizer()
}


// SetPreferredLocale allows an app to set the preferred locale for translations, overwriting the System Locale.
// locale can be in format en_US_someVariant, en_US, en-US-someVariant, en-US, en
// Since 2.6
Copy link
Member

Choose a reason for hiding this comment

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

This isn't the right format for a since line, missing : and the blank comment line above - see https://github.com/fyne-io/fyne/wiki/Contributing (or other Since: lines in the codebase).

func SetPreferredLocale(locale string) {
preferredLanguage = locale
updateLocalizer()
}

func addLanguage(data []byte, name string) error {
f, err := bundle.ParseMessageFileBytes(data, name)
translated = append(translated, f.Tag)
Expand Down Expand Up @@ -186,18 +209,52 @@
return str.String()
}

func orderLanguages(a, b language.Tag) int {
indexA := -1
indexB := -1
for i, l := range languageOrder {
if a.String() == l {
indexA = i
}
if b.String() == l {
indexB = i
}
}
// Order both languages as defined in languageOrder
if indexA != -1 && indexB != -1 {
return indexA - indexB
}
// If it is the only language in languageOrder, it comes first
if indexA != -1 {
return -1
}
if indexB != -1 {
return 1
}
// If no language is in languageOrder, sort alphabetically
return strings.Compare(a.String(), b.String())
}

// A utility for setting up languages - available to unit tests for overriding system
func setupLang(lang string) {
localizer = i18n.NewLocalizer(bundle, lang)
}

// updateLocalizer Finds the closest translation from the user's locale list and sets it up
func updateLocalizer() {
// Sort the translated slice using the orderLanguages function
slices.SortStableFunc(translated, func(a, b language.Tag) int {
return orderLanguages(a, b)
})

all, err := locale.GetLocales()
if err != nil {
fyne.LogError("Failed to load user locales", err)
all = []string{"en"}
}
if preferredLanguage != "" {
all = []string{preferredLanguage}
}
str := closestSupportedLocale(all).LanguageString()
setupLang(str)
localizer = i18n.NewLocalizer(bundle, str)
Expand Down
29 changes: 29 additions & 0 deletions lang/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@
assert.Equal(t, "Apple", XN("appleID", "Apple", 1))
assert.Equal(t, "Apples", XN("appleID", "Apple", 2))
}

func TestSetPreferredLocale(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"))
SetPreferredLocale("en")
assert.Equal(t, "Match", L("Test"))
}

func TestSetLanguageOrder(t *testing.T) {
_ = AddTranslations(fyne.NewStaticResource("en.json", []byte(`{
"Test": "Match"
}`)))
_ = AddTranslations(fyne.NewStaticResource("fr.json", []byte(`{
"Test2": "Match2"
}`)))
setupLang("xyz") // invalid language to test fallback
SetLanguageOrder("en")

Check failure on line 101 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / mobile_tests (1.21.x)

cannot use "en" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 101 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / windows_tests (1.21.x)

cannot use "en" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 101 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.21.x, ubuntu-latest)

cannot use "en" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 101 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.21.x, macos-latest)

cannot use "en" (untyped string constant) as []string value in argument to SetLanguageOrder
assert.Equal(t, "Match", L("Test"))
SetLanguageOrder("fr")

Check failure on line 103 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / mobile_tests (1.21.x)

cannot use "fr" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 103 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / windows_tests (1.21.x)

cannot use "fr" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 103 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.21.x, ubuntu-latest)

cannot use "fr" (untyped string constant) as []string value in argument to SetLanguageOrder

Check failure on line 103 in lang/lang_test.go

View workflow job for this annotation

GitHub Actions / platform_tests (1.21.x, macos-latest)

cannot use "fr" (untyped string constant) as []string value in argument to SetLanguageOrder
assert.Equal(t, "Match2", L("Test2"))
setupLang("en")
assert.Equal(t, "Match", L("Test"))
}
Loading