Repository navigation
Resources: support non-first device languages #5759
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
Merged
+589
−186
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
components/resources/demo/shared/src/commonMain/composeResources/values-nl/strings.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <resources> | ||
| <string name="hello">😊 Hallo wereld!</string> | ||
| <string name="str_template">Hallo, %1$s!\nJe hebt %2$d nieuwe berichten.</string> | ||
| <string-array name="str_arr"> | ||
| <item>onderdeel \u2605</item> | ||
| <item>onderdeel \u2318</item> | ||
| <item>onderdeel \u00BD</item> | ||
| </string-array> | ||
| </resources> |
10 changes: 10 additions & 0 deletions
10
components/resources/demo/shared/src/commonMain/composeResources/values-ru/strings.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <resources> | ||
| <string name="hello">😊 Привет, мир!</string> | ||
| <string name="multi_line">Это русский текст.\nПеревода на нидерландский для него нет.</string> | ||
| <plurals name="new_message"> | ||
| <item quantity="one">%1$d новое сообщение</item> | ||
| <item quantity="few">%1$d новых сообщения</item> | ||
| <item quantity="many">%1$d новых сообщений</item> | ||
| <item quantity="other">%1$d нового сообщения</item> | ||
| </plurals> | ||
| </resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...brary/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceLocalesComposeTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package org.jetbrains.compose.resources | ||
|
|
||
| import androidx.compose.runtime.* | ||
| import androidx.compose.ui.test.ExperimentalTestApi | ||
| import androidx.compose.ui.text.intl.Locale | ||
| import kotlin.io.encoding.Base64 | ||
| import kotlin.test.* | ||
|
|
||
| @OptIn(ExperimentalTestApi::class) | ||
| class ResourceLocalesComposeTest : KmpUiTest() { | ||
| @Test | ||
| fun secondaryLanguageChangesInvalidateCacheAndUpdatePluralRules() = clearResourceCachesAndRunUiTest { | ||
| var languages by mutableStateOf(listOf("fr", "es")) | ||
| val environment = object : ComposeEnvironment { | ||
| @Composable | ||
| override fun rememberEnvironment() = ResourceEnvironment( | ||
| languages.map { ResourceLocale(Locale(it)) }, ThemeQualifier.LIGHT, DensityQualifier.MDPI | ||
| ) | ||
| } | ||
| val readPaths = mutableListOf<String>() | ||
| val reader = object : ResourceReader { | ||
| override suspend fun read(path: String): ByteArray { | ||
| readPaths.add(path) | ||
| fun encode(value: String) = Base64.encode(value.encodeToByteArray()) | ||
| return "plurals|plural|one:${encode("$path-one")},other:${encode("$path-other")}".encodeToByteArray() | ||
| } | ||
| override suspend fun readPart(path: String, offset: Long, size: Long) = read(path) | ||
| override fun getUri(path: String): String = error("Not used") | ||
| } | ||
| val resource = PluralStringResource("secondary_plural", "secondary_plural", setOf( | ||
| ResourceItem(emptySet(), "default", 0, 0), | ||
| ResourceItem(setOf(LanguageQualifier("es")), "es", 0, 0), | ||
| ResourceItem(setOf(LanguageQualifier("de")), "de", 0, 0), | ||
| ResourceItem(setOf(LanguageQualifier("pt")), "pt", 0, 0) | ||
| )) | ||
| var text = "" | ||
| setContent { | ||
| CompositionLocalProvider(LocalComposeEnvironment provides environment, LocalResourceReader provides reader) { | ||
| text = pluralStringResource(resource, 0) | ||
| } | ||
| } | ||
| waitResources() | ||
| assertEquals("es-other", text) // French uses "one" for zero; the selected Spanish locale uses "other". | ||
| languages = listOf("fr", "de") | ||
| waitResources() | ||
| assertEquals("de-other", text) | ||
| languages = listOf("fr", "pt-PT") | ||
| waitResources() | ||
| assertEquals("pt-other", text) | ||
| languages = listOf("fr", "pt-BR") | ||
| waitResources() | ||
| assertEquals("pt-one", text) // The generic Portuguese resource uses the requested region's rules. | ||
| languages = listOf("fr", "en", "es") | ||
| waitResources() | ||
| assertEquals("default-other", text) // Implicit English wins over Spanish and uses English plural rules. | ||
| languages = listOf("en", "es") | ||
| waitResources() | ||
| assertEquals("default-other", text) | ||
| languages = listOf("fr", "ja") | ||
| waitResources() | ||
| assertEquals("default-one", text) // Without an English preference, defaults retain the primary locale's rules. | ||
| assertEquals(listOf("es", "de", "pt", "default"), readPaths) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.