Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions core/localization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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<String, Map<String, String>> = mapOf(")
Expand All @@ -112,7 +117,7 @@ kotlin {
}
commonMain {
dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(compose.runtime)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LocalizationLanguage> = localizationLanguages

/**
* Exposes the `languageCodeFlow` value used by the SDAI localization layer.
*
* @author Dmitriy Moroz
*/
val languageCodeFlow: StateFlow<String> = 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?) {
Expand All @@ -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<String, String> =
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
*/
Expand All @@ -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 {
Expand All @@ -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<out Any?>): String {
Expand All @@ -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<LocalizationLanguage>

/**
* 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<String, Map<String, String>>

/**
* 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?
Original file line number Diff line number Diff line change
Expand Up @@ -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? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<OriginalImageBudget>() })
Expand Down Expand Up @@ -132,7 +130,6 @@ fun AiSdApp(

if (shellHomeRoute != null) {
HomeRoutesScaffold(
languageCode = languageCode,
activeRoute = shellHomeRoute,
renderedRoutes = renderedHomeRoutes,
router = router,
Expand All @@ -146,7 +143,6 @@ fun AiSdApp(

if (selectedHomeRoute == null) {
OverlayRouteContent(
languageCode = languageCode,
route = currentRoute,
koin = koin,
router = router,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -93,7 +92,6 @@ import org.koin.core.parameter.parametersOf
*/
@Composable
internal fun HomeRoutesScaffold(
languageCode: String,
activeRoute: AppRoute,
renderedRoutes: List<AppRoute>,
router: RootAppRouter,
Expand Down Expand Up @@ -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.
Expand All @@ -218,7 +215,6 @@ internal fun HomeRouteSlot(
*/
@Composable
internal fun OverlayRouteContent(
languageCode: String,
route: AppRoute,
koin: Koin,
router: RootAppRouter,
Expand Down
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -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,
)
}
}
Loading
Loading