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

Bugfix finding closest supported language with app translations. #5016

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Changes from all 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
26 changes: 16 additions & 10 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ func LocalizePluralKey(key, fallback string, count int, data ...any) string {
// The language that this relates to will be inferred from the resource name, for example "fr.json".
// The data should be in json format.
func AddTranslations(r fyne.Resource) error {
defer setLocalizer()
return addLanguage(r.Content(), r.Name())
}

// AddTranslationsForLocale allows an app to load a bundle of translations for a specified locale.
// The data should be in json format.
func AddTranslationsForLocale(data []byte, l fyne.Locale) error {
defer setLocalizer()
return addLanguage(data, l.String()+".json")
}

Expand Down Expand Up @@ -152,6 +154,8 @@ func AddTranslationsFS(fs embed.FS, dir string) (retErr error) {
}
}

setLocalizer()

return retErr
}

Expand All @@ -169,16 +173,6 @@ func init() {
if err != nil {
fyne.LogError("Error occurred loading built-in translations", err)
}

// Find the closest translation from the user's locale list and set it up
all, err := locale.GetLocales()
if err != nil {
fyne.LogError("Failed to load user locales", err)
all = []string{"en"}
}
str := closestSupportedLocale(all).LanguageString()
setupLang(str)
localizer = i18n.NewLocalizer(bundle, str)
}

func fallbackWithData(key, fallback string, data any) string {
Expand All @@ -192,6 +186,18 @@ func fallbackWithData(key, fallback string, data any) string {
return str.String()
}

func setLocalizer() {
// Find the closest translation from the user's locale list and set it up
all, err := locale.GetLocales()
if err != nil {
fyne.LogError("Failed to load user locales", err)
all = []string{"en"}
}
str := closestSupportedLocale(all).LanguageString()
setupLang(str)
localizer = i18n.NewLocalizer(bundle, str)
}

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