Skip to content

Commit

Permalink
Merge pull request #5016 from Sharrnah/release/v2.5.x
Browse files Browse the repository at this point in the history
Bugfix finding closest supported language with app translations.
  • Loading branch information
dweymouth authored Jul 23, 2024
2 parents bed23d9 + 38e6752 commit 996eb30
Showing 1 changed file with 16 additions and 10 deletions.
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

0 comments on commit 996eb30

Please sign in to comment.