All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.bselzer.ktx.compose.ui.intl.Locale.kt Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package com.bselzer.ktx.compose.ui.intl

import androidx.compose.runtime.*
import com.bselzer.ktx.intl.Locale
import com.bselzer.ktx.intl.Localizer

fun com.bselzer.ktx.intl.Locale.toComposeLocale() = androidx.compose.ui.text.intl.Locale(languageTag)

/**
 * Provides the current locale.
 */
val LocalLocale: ProvidableCompositionLocal = compositionLocalOf { Localizer.locale.toComposeLocale() }

/**
 * Updates the [LocalLocale] with the [Localizer.locale].
 */
@Composable
fun ProvideLocale(localizer: Localizer = Localizer, content: @Composable () -> Unit) {
    var locale by remember { mutableStateOf(localizer.locale.toComposeLocale()) }
    CompositionLocalProvider(
        LocalLocale provides locale,
        content = content
    )

    val listener: (Locale) -> Unit = remember { { locale = it.toComposeLocale() } }
    DisposableEffect(listener) {
        locale = localizer.locale.toComposeLocale()
        Localizer.addListener(listener)
        onDispose { localizer.removeListener(listener) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy