diff --git a/core/localization/build.gradle.kts b/core/localization/build.gradle.kts index 16c678af7..64bb980b9 100644 --- a/core/localization/build.gradle.kts +++ b/core/localization/build.gradle.kts @@ -5,7 +5,7 @@ import org.w3c.dom.Element import javax.xml.parsers.DocumentBuilderFactory plugins { - alias(libs.plugins.generic.kmp.library) + alias(libs.plugins.generic.kmp.compose) } android { @@ -63,7 +63,9 @@ private fun Task.configureLocalizationCatalogTask( ?: error("Missing default localization file src/androidMain/res/values/strings.xml") val languages = languageStrings.keys - .sortedWith(compareBy({ preferredLanguageOrder.indexOf(it).takeIf { index -> index >= 0 } ?: Int.MAX_VALUE }, { it })) + .sortedWith(compareBy({ + preferredLanguageOrder.indexOf(it).takeIf { index -> index >= 0 } ?: Int.MAX_VALUE + }, { it })) val outputFile = generatedLocalizationDir.get() .file("com/shifthackz/aisdv1/core/localization/LocalizationCatalog.kt") @@ -87,7 +89,10 @@ private fun Task.configureLocalizationCatalogTask( .filterNot { it == defaultLanguageCode } .forEach { code -> appendLine() - appendStringsMap("${code.sanitizedMapName()}Strings", languageStrings.getValue(code)) + appendStringsMap( + "${code.sanitizedMapName()}Strings", + languageStrings.getValue(code) + ) } appendLine() appendLine("internal actual val localizationCatalog: Map> = mapOf(") @@ -112,7 +117,7 @@ kotlin { } commonMain { dependencies { - implementation(libs.kotlinx.coroutines.core) + implementation(compose.runtime) } } } diff --git a/core/localization/src/androidMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.android.kt b/core/localization/src/androidMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.android.kt index 75ab64e05..dfc43418a 100644 --- a/core/localization/src/androidMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.android.kt +++ b/core/localization/src/androidMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.android.kt @@ -3,24 +3,7 @@ package com.shifthackz.aisdv1.core.localization import java.util.Locale /** - * Returns the Android flag drawable associated with a language in the localization catalog. - * - * @param code ISO 639-1 language code used by the localization catalog. - * @return Flag drawable resource ID, or `null` when no flag is available for the code. - * @author Dmitriy Moroz - */ -internal actual fun countryFlagDrawableResId(code: String): Int? = when (code) { - "en" -> R.drawable.gb - "uk" -> R.drawable.ua - "tr" -> R.drawable.tr - "ru" -> R.drawable.ru - "zh" -> R.drawable.cn - "de" -> R.drawable.de - else -> null -} - -/** - * Executes the `platformLanguageCode` step in the SDAI localization layer. + * Reads the preferred language reported by the native host. * * @author Dmitriy Moroz */ diff --git a/core/localization/src/commonMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.kt b/core/localization/src/commonMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.kt index 642e2278e..76b0a23e5 100644 --- a/core/localization/src/commonMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.kt +++ b/core/localization/src/commonMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.kt @@ -1,76 +1,58 @@ package com.shifthackz.aisdv1.core.localization -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow +import androidx.compose.runtime.mutableStateOf /** - * Carries `LocalizationLanguage` data through the SDAI localization layer. + * A supported application language displayed in the language picker. + * + * @param code Normalized language code used to select the bundled catalog. + * @param name Native language name displayed independently of the selected locale. * * @author Dmitriy Moroz */ data class LocalizationLanguage( - /** - * Exposes the `code` value used by the SDAI localization layer. - * - * @author Dmitriy Moroz - */ val code: String, - /** - * Exposes the `name` value used by the SDAI localization layer. - * - * @author Dmitriy Moroz - */ val name: String, ) /** - * Provides the `Localization` singleton used by the SDAI localization layer. + * Resolves bundled translations using the selected application language. + * Language reads participate in Compose snapshot observation, so a locale change invalidates + * each composition scope that resolved text, including retained tabs and lazy list items. * * @author Dmitriy Moroz */ object Localization { /** - * Exposes the `DEFAULT_LANGUAGE_CODE` value used by the SDAI localization layer. + * Fallback catalog used when a language or translation key is unavailable. * * @author Dmitriy Moroz */ const val DEFAULT_LANGUAGE_CODE = "en" - /** - * Exposes the `languageCodeState` value used by the SDAI localization layer. - * - * @author Dmitriy Moroz - */ - private val languageCodeState = MutableStateFlow(supportedLanguageCode(platformLanguageCode())) + private val languageCodeState = mutableStateOf(supportedLanguageCode(platformLanguageCode())) /** - * Exposes the `entries` value used by the SDAI localization layer. + * Supported languages in the catalog order used by the language picker. * * @author Dmitriy Moroz */ val entries: List = localizationLanguages /** - * Exposes the `languageCodeFlow` value used by the SDAI localization layer. - * - * @author Dmitriy Moroz - */ - val languageCodeFlow: StateFlow = languageCodeState.asStateFlow() - - /** - * Executes the `currentLanguageCode` step in the SDAI localization layer. + * Returns the active normalized language code and observes it when read during composition. * - * @return Result produced by `currentLanguageCode`. + * @return Supported language code for the active catalog. * @author Dmitriy Moroz */ fun currentLanguageCode(): String = languageCodeState.value /** - * Executes the `setLanguageCode` step in the SDAI localization layer. + * Selects a bundled language and immediately invalidates observing composition scopes. + * Re-selecting the current language leaves those scopes unchanged. * - * @param code code value consumed by the API. + * @param code Language code to normalize; null selects the platform language. * @author Dmitriy Moroz */ fun setLanguageCode(code: String?) { @@ -79,20 +61,21 @@ object Localization { } /** - * Executes the `strings` step in the SDAI localization layer. + * Returns the bundled catalog for a language, falling back to English if unsupported. * * @param languageCode BCP-47 language code handled by the platform layer. - * @return Result produced by `strings`. + * @return Translation keys mapped to their localized text. * @author Dmitriy Moroz */ fun strings(languageCode: String = currentLanguageCode()): Map = localizationCatalog.getValue(supportedLanguageCode(languageCode)) /** - * Executes the `string` step in the SDAI localization layer. + * Resolves a translation and substitutes sequential or numbered string placeholders. + * Missing translations fall back to English, then to the key itself. * - * @param key key value consumed by the API. - * @param args args value consumed by the API. + * @param key Translation key from the bundled catalog. + * @param args Values substituted for `%s` or numbered `%1$s` placeholders. * @param languageCode BCP-47 language code handled by the platform layer. * @author Dmitriy Moroz */ @@ -105,18 +88,10 @@ object Localization { .format(args) /** - * Loads SDAI data through `getCountryFlagDrawableResId`. - * - * @param code code value consumed by the API. - * @author Dmitriy Moroz - */ - fun getCountryFlagDrawableResId(code: String): Int? = countryFlagDrawableResId(code) - - /** - * Executes the `supportedLanguageCode` step in the SDAI localization layer. + * Normalizes a locale to a bundled base language, with English as the fallback. * - * @param code code value consumed by the API. - * @return Result produced by `supportedLanguageCode`. + * @param code Locale code to normalize; null and unsupported codes select English. + * @return Supported base language code. * @author Dmitriy Moroz */ private fun supportedLanguageCode(code: String?): String { @@ -132,10 +107,10 @@ object Localization { } /** - * Executes the `format` step in the SDAI localization layer. + * Substitutes numbered placeholders before consuming sequential string placeholders. * - * @param args args value consumed by the API. - * @return Result produced by `format`. + * @param args Values substituted for `%s` or numbered `%1$s` placeholders. + * @return Text with supplied placeholder values substituted. * @author Dmitriy Moroz */ private fun String.format(args: Array): String { @@ -151,32 +126,23 @@ object Localization { } /** - * Exposes the `localizationLanguages` value used by the SDAI localization layer. + * Platform catalog languages and native names generated from the canonical string resources. * * @author Dmitriy Moroz */ internal expect val localizationLanguages: List /** - * Exposes the `localizationCatalog` value used by the SDAI localization layer. + * Generated platform translations keyed by normalized language code and translation key. * * @author Dmitriy Moroz */ internal expect val localizationCatalog: Map> /** - * Executes the `countryFlagDrawableResId` step in the SDAI localization layer. - * - * @param code code value consumed by the API. - * @return Result produced by `countryFlagDrawableResId`. - * @author Dmitriy Moroz - */ -internal expect fun countryFlagDrawableResId(code: String): Int? - -/** - * Executes the `platformLanguageCode` step in the SDAI localization layer. + * Reads the preferred language supplied by the native host. * - * @return Result produced by `platformLanguageCode`. + * @return Platform locale code, or null when unavailable. * @author Dmitriy Moroz */ internal expect fun platformLanguageCode(): String? diff --git a/core/localization/src/iosMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.ios.kt b/core/localization/src/iosMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.ios.kt index abfd79050..6322d0503 100644 --- a/core/localization/src/iosMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.ios.kt +++ b/core/localization/src/iosMain/kotlin/com/shifthackz/aisdv1/core/localization/Localization.ios.kt @@ -3,18 +3,9 @@ package com.shifthackz.aisdv1.core.localization import platform.Foundation.NSUserDefaults /** - * Executes the `countryFlagDrawableResId` step in the SDAI localization layer. + * Reads the preferred language reported by the native host. * - * @param code code value consumed by the API. - * @return Result produced by `countryFlagDrawableResId`. - * @author Dmitriy Moroz - */ -internal actual fun countryFlagDrawableResId(code: String): Int? = null - -/** - * Executes the `platformLanguageCode` step in the SDAI localization layer. - * - * @return Result produced by `platformLanguageCode`. + * @return Native language code, or null when the host has no preferred language. * @author Dmitriy Moroz */ internal actual fun platformLanguageCode(): String? = diff --git a/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdApp.kt b/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdApp.kt index be5a530d1..d5bb9da79 100644 --- a/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdApp.kt +++ b/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdApp.kt @@ -30,7 +30,6 @@ import androidx.lifecycle.compose.LifecycleEventEffect import com.shifthackz.aisdv1.presentation.components.extensions.generationBrightnessInteraction import com.shifthackz.aisdv1.core.common.appbuild.BuildInfoProvider import com.shifthackz.aisdv1.core.extensions.clearFocusOnTap -import com.shifthackz.aisdv1.core.localization.Localization import com.shifthackz.aisdv1.domain.preference.PreferenceManager import org.koin.mp.KoinPlatform.getKoin import com.shifthackz.aisdv1.presentation.components.platform.rememberExternalUrlLauncher @@ -63,7 +62,6 @@ fun AiSdApp( val route by router.route.collectAsState() val isDrawerOpen by router.drawerOpen.collectAsState() val canNavigateBack by router.canNavigateBack.collectAsState() - val languageCode by Localization.languageCodeFlow.collectAsState() val urlLauncher = rememberExternalUrlLauncher() val imageToImagePlatformActions = rememberImageToImagePlatformActions(remember(koin) { koin.get() }) @@ -132,7 +130,6 @@ fun AiSdApp( if (shellHomeRoute != null) { HomeRoutesScaffold( - languageCode = languageCode, activeRoute = shellHomeRoute, renderedRoutes = renderedHomeRoutes, router = router, @@ -146,7 +143,6 @@ fun AiSdApp( if (selectedHomeRoute == null) { OverlayRouteContent( - languageCode = languageCode, route = currentRoute, koin = koin, router = router, diff --git a/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdAppRoutes.kt b/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdAppRoutes.kt index 67bda23aa..b8f4c7f0d 100644 --- a/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdAppRoutes.kt +++ b/presentation/app/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/app/AiSdAppRoutes.kt @@ -78,7 +78,6 @@ import org.koin.core.parameter.parametersOf * Standalone overlay routes such as storage usage and network usage are deliberately excluded so * they can render without the bottom bar. * - * @param languageCode Locale revision carried through the root’s captured content lambdas. * @param modifier Layout modifiers for the retained home scaffold. * @param activeRoute Route currently visible in the home scaffold. * @param renderedRoutes Home routes already created and kept alive. @@ -93,7 +92,6 @@ import org.koin.core.parameter.parametersOf */ @Composable internal fun HomeRoutesScaffold( - languageCode: String, activeRoute: AppRoute, renderedRoutes: List, router: RootAppRouter, @@ -207,7 +205,6 @@ internal fun HomeRouteSlot( /** * Renders routes that sit above the home scaffold and own their full-screen layout. * - * @param languageCode Locale revision carried through the root’s captured content lambdas. * @param route Route currently requested by the root router. * @param koin Presentation Koin instance used to resolve route dependencies. * @param router Root router passed to overlay screens. @@ -218,7 +215,6 @@ internal fun HomeRouteSlot( */ @Composable internal fun OverlayRouteContent( - languageCode: String, route: AppRoute, koin: Koin, router: RootAppRouter, diff --git a/presentation/components/src/androidMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.android.kt b/presentation/components/src/androidMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.android.kt index 4391fd145..4092009c0 100644 --- a/presentation/components/src/androidMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.android.kt +++ b/presentation/components/src/androidMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.android.kt @@ -1,11 +1,5 @@ package com.shifthackz.aisdv1.presentation.components.modal.language -import androidx.compose.foundation.Image -import androidx.compose.foundation.layout.padding -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.res.painterResource -import androidx.compose.ui.unit.dp import com.shifthackz.aisdv1.core.localization.Localization /** @@ -18,22 +12,3 @@ import com.shifthackz.aisdv1.core.localization.Localization actual fun applyAppLanguage(languageCode: String) { Localization.setLanguageCode(languageCode) } - -/** - * Draws a bundled Android flag when the language has a matching drawable. - * - * @param languageCode Supported application localization code. - * @param modifier Size and placement of the flag. - * - * @author Dmitriy Moroz - */ -@Composable -internal actual fun LanguageFlagIcon(languageCode: String, modifier: Modifier) { - Localization.getCountryFlagDrawableResId(languageCode)?.let { drawableResId -> - Image( - modifier = modifier.padding(horizontal = 8.dp), - painter = painterResource(id = drawableResId), - contentDescription = languageCode, - ) - } -} diff --git a/core/localization/src/androidMain/res/drawable/cn.png b/presentation/components/src/commonMain/composeResources/drawable/flag_cn.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/cn.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_cn.png diff --git a/core/localization/src/androidMain/res/drawable/de.png b/presentation/components/src/commonMain/composeResources/drawable/flag_de.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/de.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_de.png diff --git a/core/localization/src/androidMain/res/drawable/gb.png b/presentation/components/src/commonMain/composeResources/drawable/flag_gb.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/gb.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_gb.png diff --git a/core/localization/src/androidMain/res/drawable/ru.png b/presentation/components/src/commonMain/composeResources/drawable/flag_ru.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/ru.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_ru.png diff --git a/core/localization/src/androidMain/res/drawable/tr.png b/presentation/components/src/commonMain/composeResources/drawable/flag_tr.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/tr.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_tr.png diff --git a/core/localization/src/androidMain/res/drawable/ua.png b/presentation/components/src/commonMain/composeResources/drawable/flag_ua.png similarity index 100% rename from core/localization/src/androidMain/res/drawable/ua.png rename to presentation/components/src/commonMain/composeResources/drawable/flag_ua.png diff --git a/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageBottomSheet.kt b/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageBottomSheet.kt index 087135ae0..69650ce14 100644 --- a/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageBottomSheet.kt +++ b/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageBottomSheet.kt @@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.navigationBarsPadding import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.shifthackz.aisdv1.core.localization.Localization @@ -12,7 +13,7 @@ import com.shifthackz.aisdv1.core.model.asUiText import com.shifthackz.aisdv1.presentation.components.widget.item.SettingsItem /** - * Lists supported languages and highlights the current application locale. + * Lists supported languages alphabetically by their native display names and highlights the active locale. * Selecting a row reports its language code and then requests dismissal. * * @param modifier Layout modifiers for the language list. @@ -27,6 +28,7 @@ fun LanguageBottomSheet( onLanguageSelected: (String) -> Unit = { applyAppLanguage(it) }, onDismissRequest: () -> Unit = {}, ) { + val languages = remember { Localization.entries.sortedBy { it.name.lowercase() } } Column( modifier = modifier .fillMaxWidth() @@ -34,7 +36,7 @@ fun LanguageBottomSheet( .navigationBarsPadding() .padding(bottom = 16.dp), ) { - Localization.entries.forEach { language -> + languages.forEach { language -> val locale = language.code SettingsItem( modifier = Modifier @@ -63,14 +65,3 @@ fun LanguageBottomSheet( * @author Dmitriy Moroz */ expect fun applyAppLanguage(languageCode: String) - -/** - * Draws the bundled flag corresponding to a supported localization code. - * - * @param languageCode Code identifying the language's bundled flag. - * @param modifier Size and placement of the flag within its row. - * - * @author Dmitriy Moroz - */ -@Composable -internal expect fun LanguageFlagIcon(languageCode: String, modifier: Modifier = Modifier) diff --git a/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageFlagIcon.kt b/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageFlagIcon.kt new file mode 100644 index 000000000..7ba448fdd --- /dev/null +++ b/presentation/components/src/commonMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguageFlagIcon.kt @@ -0,0 +1,42 @@ +package com.shifthackz.aisdv1.presentation.components.modal.language + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.shifthackz.aisdv1.presentation.components.generated.resources.Res +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_cn +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_de +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_gb +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_ru +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_tr +import com.shifthackz.aisdv1.presentation.components.generated.resources.flag_ua +import org.jetbrains.compose.resources.painterResource + +/** + * Renders the same bundled flag at a fixed size on Android and iOS language rows. + * Unknown language codes omit the icon; the adjacent native language name labels each row. + * + * @param languageCode Supported language code identifying the bundled flag. + * @param modifier Size and placement modifiers applied before the default flag spacing. + * @author Dmitriy Moroz + */ +@Composable +internal fun LanguageFlagIcon(languageCode: String, modifier: Modifier = Modifier) { + val flag = when (languageCode) { + "en" -> Res.drawable.flag_gb + "uk" -> Res.drawable.flag_ua + "tr" -> Res.drawable.flag_tr + "ru" -> Res.drawable.flag_ru + "zh" -> Res.drawable.flag_cn + "de" -> Res.drawable.flag_de + else -> return + } + Image( + modifier = modifier.padding(horizontal = 8.dp).size(width = 32.dp, height = 24.dp), + painter = painterResource(flag), + contentDescription = null, + ) +} diff --git a/presentation/components/src/iosMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.ios.kt b/presentation/components/src/iosMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.ios.kt index b39e96652..ca0b0ae0f 100644 --- a/presentation/components/src/iosMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.ios.kt +++ b/presentation/components/src/iosMain/kotlin/com/shifthackz/aisdv1/presentation/components/modal/language/LanguagePlatform.ios.kt @@ -1,8 +1,5 @@ package com.shifthackz.aisdv1.presentation.components.modal.language -import androidx.compose.ui.Modifier - -import androidx.compose.runtime.Composable import com.shifthackz.aisdv1.core.localization.Localization import platform.Foundation.NSUserDefaults @@ -21,14 +18,3 @@ actual fun applyAppLanguage(languageCode: String) { ) NSUserDefaults.standardUserDefaults.synchronize() } - -/** - * Reserves no flag content on iOS; the language name remains the selection affordance. - * - * @param languageCode Supported application localization code. - * @param modifier Layout modifiers accepted for platform API parity. - * - * @author Dmitriy Moroz - */ -@Composable -internal actual fun LanguageFlagIcon(languageCode: String, modifier: Modifier) = Unit