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

stringResource filters the default when getting the resource path #4571

Closed
Zhou-Kt opened this issue Apr 3, 2024 · 5 comments · Fixed by #4577
Closed

stringResource filters the default when getting the resource path #4571

Zhou-Kt opened this issue Apr 3, 2024 · 5 comments · Fixed by #4577
Assignees
Labels
bug Something isn't working p:high High priority resources

Comments

@Zhou-Kt
Copy link

Zhou-Kt commented Apr 3, 2024

Describe the bug
Hey, I've found that on Android or Desktop, in org.jetbrains.compose.resources.ResourcesEnvironment.kt , the extension function getPathByEnvironment of Resource class will filter the default StringResource (values/strings.xml) by filterBy. For example, I have default (its qualifiers is an empty Set), zh-HK, zh-TW and zh-MO in StringResource.items, and the platform language is zh-CN, the first filterBy will filter out zh-HK, zh-TW, and zh-MO, and the second filterBy will return an empty List because CN cannot be found, and finally cause an exception, instead of returning the default path. I have given a modification (it works in this case) in screenshot 2 below.
(My English is poor, wish I expressed it clearly enough.)

Affected platforms

  • Desktop (Windows, Linux, macOS)
  • Android
  • other platforms, may be

Versions

  • Kotlin version*: 1.9.23
  • Compose Multiplatform version*: 1.6.1

Screenshots
Snipaste_2024-04-03_14-26-42
Snipaste_2024-04-03_14-27-39

@terrakok
Copy link
Collaborator

terrakok commented Apr 4, 2024

In a case when you provide region's specific localizations, you are suppose to provide default localization without the region (in your case values-zh). To get the default without any qualifier is not right because it can be an absolutely different locale.

The same loggic is being used in the Android:
https://developer.android.com/guide/topics/resources/providing-resources#BestMatch

image

@Zhou-Kt
Copy link
Author

Zhou-Kt commented Apr 4, 2024

Okay, thank you very much, it's my mistake, i'm following the Android Resource approach, whose default string resource doesn't need to qualify "-zh" to values.

@Zhou-Kt Zhou-Kt closed this as completed Apr 4, 2024
@terrakok
Copy link
Collaborator

terrakok commented Apr 4, 2024

Oh! I guess I got your point: the android logic works with a locale+region as a single qualifier. I need a time to think about it

@terrakok terrakok reopened this Apr 4, 2024
@Zhou-Kt
Copy link
Author

Zhou-Kt commented Apr 4, 2024

Oh! I guess I got your point: the android logic works with a locale+region as a single qualifier. I need a time to think about it

Yes, since I only use zh, changing values to values-zh would have solved my case, but if there are multiple locales, lazy people like me would be more inclined to just create one default values for all locales.

@Thomas-Vos
Copy link
Contributor

Same issue here. Strings in values/ are English for my app. I added values-en-rGB/ for UK English. Now when your phone is set to English with any other region it crashes. It should fallback to values/ because there is no values-en/.

@terrakok terrakok added the p:high High priority label Apr 4, 2024
terrakok added a commit that referenced this issue Apr 5, 2024
…gion or default language match (#4577)

We need to filter by language and region together because there is
slightly different logic:
1) if there is the exact match language+region then use it
2) if there is the language WITHOUT region match then use it
3) in other cases use items WITHOUT language and region qualifiers at
all

Given resources:
values
values-en-rUS
values-de

Filter results:
"en" -> values
"en-US" -> values-en-rUS
"en-GB" -> values
"de-IT" -> values-de

fixes #4571
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p:high High priority resources
Projects
None yet
4 participants