-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
String resources for internationalization (i18n) #425
Comments
Image viewer example has some code for localization. |
Where does R.kt come from? How does it handle plurals? Image viewer has a common subproject but then it splits much of its code out in to separate implementations for Android https://github.com/JetBrains/compose-jb/blob/da611b319dfa9cb0141d60ad5687b1771854ec72/examples/imageviewer/common/src/androidMain/kotlin/example/imageviewer/view/MainScreen.kt#L202-L210 Those look like they're supposed to be doing the same job, but the way they use string resources is entirely different. Can we write a view that will work in either environment? |
Generally, while meaningful, not sure if it fully belongs to the UI framework scope. |
@igordmn do you have an idea how we could support in MPP manner? |
One of the complex things is pluraziation support. Java only provides ChoiceFormat, which properly works only with some languages. Proper pluraziation support implemented in ICU. We can provide a function for pluralization in skiko (because skia already uses icu library). Or write a pluralization function in pure Kotlin/Java using patterns (example) Another thing we need to think about - how string resources should be defined:
Don't know if it is a good idea. It maybe convinient for string formatting, but not convinient for translators.
|
Yeah. Assume translators and their tools don't know Kotlin at all. Best to have a format that makes it super clear which things are strings to be translated and which aren't. Using one of the web translation portals or other Computer Assisted Translation tools is going to want to parse it in to something like
A good translation house is used to taking all sorts of inputs and munging them in to something that will work but you'll have more options (and cheaper ones) if you can use an existing format. Especially when you take escaping rules in to account: java
One question that might help clarify this: Is it a goal to be able to share views like that MainScreen.kt between android and Compose for Desktop? Or are Compose for Desktop apps really their own thing? Sometimes you might get lucky and be able to use some android-compatible component (because that's one of the benefits of composability), but you expect a desktop app to be written as a desktop app. I've been more of a desktop dev than a mobile one, but my hunch is that if you want to pitch this as a way to take a design you've written for an Android tablet and use it on a Windows or Wayland touchscreen (or vice-versa), then views will need to be compatible with android's string resources. (As a desktop dev, android support is of secondary importance to me. But android's i18n tools look far more capable than what we've seen from the JDK, as igordmn pointed out with ChoiceFormat. So when I saw this project talking about bringing |
@igordmn would migrating to |
Yes, I think that will fix the issue. Thanks 👍! |
@igordmn, @keturn just migrated Moko-Resources from |
moko-resource is good but there is some issue with cache configuration issue , if it was created using ksp then it would be amazing |
Most certainly YES. |
This feature is very important for KMP ecosystem. There are others libraries with that goal but they generally have issues with kotlin and/or compose-multiplatform updates. |
Is there any plan to support plural in the new resource library? |
Ports a part of Unicode's ICU in pure Kotlin and implements Android-style plural string resource support. Fixes #425. # Changes - Added `org.jetbrains.compose.resources.intl.{PluralCategory, PluralRule, PluralRuleList}`, which parses and evaluates scripts in Unicode's Locale Data Markup Langauge. - Copied `plurals.xml` from Unicode's [CLDR](https://github.com/unicode-org/cldr/blob/release-44-1/common/supplemental/plurals.xml). - Added `GeneratePluralRuleListsTask`, which parses `plurals.xml` and generates required Kotlin source codes. - Added `PluralStringResource`, `pluralStringResource`, or `getPluralString`, corresponding to `StringResource`, `stringResource`, or `getString`. - Modified `ResourcesSpec.kt` so the generated `Res` class exposes `Res.plurals`. # Potential Further Improvements - [ ] Allow configuring the default language in the `compose.resources {}` block (#4482) to determine the default pluralization rule (or just presume English as default) - [ ] Move the parser logic to the Gradle plugin and generate pluralization rules in `Res` only for languages used in `composeResources` --------- Co-authored-by: Konstantin Tskhovrebov <konstantin.tskhovrebov@jetbrains.com>
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks. |
I'd like to see an example of how to do internationalization for Compose Desktop.
I looked at the Code Viewer example, as it's written to run under both Android and desktop. It doesn't yet address that feature; its Text widgets and contentDescription values are hard-coded, e.g. https://github.com/JetBrains/compose-jb/blob/4de10a649910053ed634429b88c82541989bf332/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt#L28-L29
Can we use
stringResource
(fromandroidx.compose.ui.res
) on desktop? How do we get the resource ID if we're developing for desktop and not using Android Studio?The text was updated successfully, but these errors were encountered: