Pantrix UI

The app's own words

Why, in a Compose Multiplatform app whose reader chooses its language, the components switch to the language chosen while the app's own words stay in the device's, and two ways of taking the app's words along. The problem is not Pantrix's own: it is seen the same way in every Compose Multiplatform app that lets its reader choose a language other than the device's.

What is said of Compose's resources was read in the sources of Compose Multiplatform 1.12.2 (components-resources) or seen in an app built on the library; what was seen where is written in section 6.

1. The problem

Symptom

Cause

2. The solution

WayWhat it givesWhat it costs
AThe app's words in Kotlin, chosen by the theme's language. One object of words for each language, as PantrixStrings isThe language changes at once and the same on every platform; a test chooses it with the theme; nothing of the platform is changedThe words leave Compose's resources
BCompose's resources, with the platform's language changed. Each platform's own value of the language is set to the reader's choice, and the composition is built againThe words stay in the resourcesA piece for each platform, two values on the web, and the platform's language changed for the whole app

3. With Pantrix: the words in Kotlin

3.1. The words

An interface holds what the app says; an object for each language says it. The theme's language chooses the object:

interface AppWords {
    val save: String
    val ordersTitle: String
    fun ordersCount(count: Int): String
}

object EnglishWords : AppWords {
    override val save = "Save"
    override val ordersTitle = "Orders"
    override fun ordersCount(count: Int) = if (count == 1) "1 order" else "$count orders"
}

object TurkishWords : AppWords {
    override val save = "Kaydet"
    override val ordersTitle = "Siparişler"
    override fun ordersCount(count: Int) = "$count sipariş"
}

/** The words of the nearest theme's language. */
val appWords: AppWords
    @Composable @ReadOnlyComposable
    get() = if (PantrixTheme.locale.language == "tr") TurkishWords else EnglishWords

3.2. The chosen language

The reader's choice is kept by the app, and given to the outermost theme:

@Composable
fun App(settings: Settings) {
    val chosen by settings.language.collectAsState() // null: the device's
    PantrixTheme(locale = chosen ?: systemPantrixLocale()) { Home() }
}

Pantrix's components and the app's words then speak the same language, and the writing's direction follows it.

3.3. Testing

PantrixTestTheme(locale = PantrixLocale.Turkish) draws the app's words in Turkish too, since they are chosen by the theme's language. A test of a word looks for it in one language and finds the control by its tag in any.

4. Without Pantrix's theme: compose's resources

Compose's resources follow the platform's language, so the app sets that language to the reader's choice, on each platform in its own way, and builds the composition again with key(language) { … }:

PlatformWhere the language is set
AndroidLocale.setDefault(locale), and a configuration with that locale for the resources
The JVMLocale.setDefault(locale)
iOSNSUserDefaults.standardUserDefaults.setObject(listOf(tag), forKey = "AppleLanguages")
The webA script in the page that gives navigator.languages and navigator.language the chosen tag: both, or what is read outside the composition stays in the browser's language

This is the way JetBrains documents for Compose Multiplatform's resources (LocalAppLocale). Pantrix's own words follow PantrixTheme.locale whichever way the app's take: the app gives the same language to the theme.

5. Pitfalls

6. Verification

Looked at by hand

Not verified

7. Where it is in this repository