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

Add localization support 1 #226

Merged
merged 23 commits into from
Dec 21, 2024
Merged

Conversation

hydra
Copy link
Contributor

@hydra hydra commented Nov 28, 2024

Current state: work-in-progress.

TODO:

  • API documentation.
  • Additional testing.
  • PR review and changes.

Initial cut of localization support using Fluent.

See examples/localized.rs.

Features:

  • When translations are missing, the key is displayed.
  • The first translation specified becomes the default when the system local doesn't match.
  • Widgets can be wrapped in Localized to allow for multiple translations to be used at the same time. Use-case is a form designer or report viewer where the main application is in one-language, but you want to preview a form or report in a different language. It works in the same way to being able to override the theme for a widget.
  • Supports dynamic translation arguments.

Screenshots:

image

In this screenshot, the window uses the default locale, and the first container shows a label using the window's default locale.
The second container shows a label using a specific locale. The third container is using uses a dynamic locale which can be changed using the radio buttons. It has buttons which update a dynamic value which is used as an argument for a translation.

image

In this screenshot, the language was changed to "Spanish (Spain)" and the "+" button was clicked once. Here we can see the translations were used.

Fluent translation file for 'es-ES' in the screenshots above is as follows:

message-hello-world = ¡Hola, mundo!
language-en-gb = Inglés (Gran Bretaña)
language-en-us = Inglés (Estados Unidos)
language-es-es = Español (España)
banana-counter-message =
    { $bananas_counter ->
        [one] Hay un plátano.
        *[other] Tienes { $bananas_counter } plátanos.
    }

image

In this screenshot, we can see what the UI looks like when no translations are provided.

Limitations:

  • Translations are currently re-parsed for each opened window. Some translations cache could be used in the future.
  • Window titles do not currently support localization.

Acknowledgements:

@hydra hydra force-pushed the add-localization-support-1 branch from 990101e to 571b93b Compare November 28, 2024 17:41
Copy link
Member

@ecton ecton left a comment

Choose a reason for hiding this comment

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

These changes should hopefully fix the layout invalidation issues reported on Discord.

hydra added a commit to hydra/cushy that referenced this pull request Nov 28, 2024
hydra added a commit to hydra/cushy that referenced this pull request Nov 28, 2024
@hydra hydra force-pushed the add-localization-support-1 branch from dbb42f6 to 15343ac Compare November 28, 2024 18:16
@hydra hydra force-pushed the add-localization-support-1 branch from d5d8d3e to c655883 Compare December 1, 2024 18:34
hydra added a commit to hydra/cushy that referenced this pull request Dec 10, 2024
This refactor attempts to address these concerns from the current
implementation:

- Window title and other strings needed a new type that could either be
  a hardcoded string or a localized string.
- The Window title needs to be evaluated in a context where
  DynamicDisplay isn't possible: the window itself isn't a widget, and
  so a WidgetContext can't be created to pass into DynamicDisplay.
- The Fluent bundle was reparsed on a per-window basis
- The fallback logic only ever created a fallback for the system locale.
  When there is an overridden locale, we need to support looking up into
  fallback locales that might match.
- Fallback logic needed to look in all possible fallback bundles, not
  just the first bundle that exists.
- This feature adds quite a few dependencies, so it now has a feature
  flag enabling and disabling it.
@ecton
Copy link
Member

ecton commented Dec 20, 2024

I've done a major refactoring on this to introduce the MaybeLocalized type and attempt to round out some of the localization support the way I've envisioned it working. I haven't gone through and updated the doc comments for everything yet, so some of the functionality may be slightly different than the documentation currently.

The example has been updated and that's probably the best place to see the diff of most of the API changes: 64d06a5#diff-42a98171141d7e7760d41d679d6f551d906f60bcf7ff44babd6c577c514c923f

- IntoValue conveniences for localization args
- MakeWidgetWithTag for Localize, removing all need for into_label
  usages
- Renamed localized to localized_in.
Removed all references to "translations" and wrote overview
documentation
@ecton
Copy link
Member

ecton commented Dec 21, 2024

I've finished these changes and am going to get this merged. If you have any feedback on my updates, please don't hesitate to either comment here, chat on Discord, or open up new issues! Thank you for getting this work started!

@ecton ecton merged commit d0c2704 into khonsulabs:main Dec 21, 2024
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