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

Selective translation loading #5390

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

aksdb
Copy link

@aksdb aksdb commented Jan 10, 2025

Description:

At the moment, fyne loads all base translations, sets the default language to English and merges in translations provided by the user.

This approach has a few downsides, though:

  1. If the user registers only a subset (or a different set) of languages, than the base translations provided, the closestSupportedLocale function is not (and cannot be) aware of this. It might pick a language that has only partial (our base) translations and misses user translations. Example: the user provides custom translations for English but the app runs on a German system. Some buttons and menu items are now translated into German, but all others fall back to the provided English translation.

  2. The user may not develop their app with English in mind, so falling back to English might not be the best choice for them; especially when paired with the issue above.

This PR proposes a solution to both issues that is (mostly) backwards compatible:

  • We still load all base translations and consider them "available" for matching in closestSupportedLocale. So for apps without any custom translations, everything behaves as before.
  • Once the user adds their first own translation, we reset the known languages and re-initialize the bundle.
    • The first language becomes the new fallback language. This gives the user (implicit) control, by allowing them to register the languages in their preferred order.
    • The list of known languages is reduced to the ones registered explicitly. So if, as in the example above, the user registers a custom translation for English, only English will be considered until they also register further translations for other languages.

The implementation also has two downsides, but IMO smaller ones:

  • That the first added translation becomes the default language is more or less implicit. When using AddTranslationsFS as the only (or at least first) call, one would have to make sure, that the order makes sense. It can easily be circumvented by either using file name prefixes in that case or by calling one of the other AddTranslations* functions first (with an empty JSON).

    Note: Potential improvement: offer an API to explicitly set the order of languages; overwriting the translations slice.

  • If someone relied on base translations being in place, while they register their own, different, translations, this PR would change behavior; I think it changes for the better, though and IMO it prevents (likely) unexpected side-effects.

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

@aksdb aksdb changed the title Feature/selective lang loading Selective translation loading Jan 10, 2025
@coveralls
Copy link

Coverage Status

coverage: 59.261% (-0.1%) from 59.374%
when pulling 6da8158 on aksdb:feature/selective_lang_loading
into f2ba049 on fyne-io:develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants