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

Localize internal strings in ui, material and material3 #1158

Merged
merged 9 commits into from
Mar 12, 2024

Conversation

m-sasha
Copy link

@m-sasha m-sasha commented Mar 4, 2024

Currently the user-visible strings Compose Multiplatform uses internally (e.g. "Copy" and "Paste" in context menus) are always in English. This PR adds translations of these strings to all the locales Android has localization for at https://github.com/androidx/androidx/tree/androidx-main/compose/ui/ui/src/androidMain/res

The translations are obtained by a Gradle task that check out the corresponding repository, reads the respective strings.xml files and writes Kotlin code with the translations.

Limitations:

  1. The translation is done according to the system locale and is not refreshed if it changes. Users will need to restart the app after changing the system locale.
  2. There is no facility for the developer to override the localized strings.
  3. There is no facility for the developer to specify the locale to parts of the UI, or even globally. The only point of control the developer has is, on JVM targets, to set Locale.setDefault before showing the UI.
  4. The "framework" for translating is repeated (copy/pasted) in each module. We could try extracting it into a separate module, but I didn't feel it was worth the effort. It's only a few functions.

Note that this is a temporary solution until the official resources library is stable.

Note for changelog

To disable the localization of internal Compose strings and use the English version, set the system locale before showing any UI:

  • Desktop: java.util.Locale.setDefault(java.util.Locale("en"))
  • iOS:
  • Web:

Proposed Changes

  1. Update the ui module to use a "Strings" mechanism similar to how localization is done in material and material3 modules.
  2. Set up a mechanism for strings to be translated according to the system locale.
  3. Implement a Gradle task that pulls translations from the Android/Compose repo and writes Kotlin files with the translations.
  4. Use the Gradle task to generate translations for ui, material and material3 modules.

I recommend reviewing this PR commit-by-commit.

Testing

Test: Manually, and with some new unit tests.

Issues Fixed

Fixes: JetBrains/compose-multiplatform#3360

@m-sasha m-sasha requested review from terrakok and igordmn March 4, 2024 12:19
@m-sasha m-sasha force-pushed the m-sasha/internal-localization branch from 127b3fd to 3156c0d Compare March 4, 2024 12:27
@m-sasha
Copy link
Author

m-sasha commented Mar 11, 2024

@terrakok @igordmn ping

@igordmn igordmn removed the request for review from terrakok March 11, 2024 10:04
Copy link
Collaborator

@igordmn igordmn left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@m-sasha m-sasha merged commit 4cdf43b into jb-main Mar 12, 2024
6 checks passed
@m-sasha m-sasha deleted the m-sasha/internal-localization branch March 12, 2024 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants