-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
i18n/src/iosMain/kotlin/com/prof18/feedflow/i18n/TranslationExtensions.kt
This file contains 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,29 @@ | ||
package com.prof18.feedflow.i18n | ||
|
||
import dev.icerock.moko.resources.StringResource | ||
import dev.icerock.moko.resources.desc.ResourceStringDesc | ||
import dev.icerock.moko.resources.desc.StringDesc | ||
|
||
|
||
fun ResourceStringDesc.localizedWithFallback(): String { | ||
return localizedStringWithFallback(stringRes) | ||
} | ||
|
||
private const val FALLBACK_LOCALE = "en" | ||
|
||
private fun localizedStringWithFallback(stringRes: StringResource): String { | ||
val bundle = StringDesc.localeType.getLocaleBundle(stringRes.bundle) | ||
val stringInCurrentLocale = bundle.localizedStringForKey(stringRes.resourceId, null, null) | ||
return if (stringInCurrentLocale == stringRes.resourceId) { | ||
val stringInDefaultBundle = stringRes.bundle.localizedStringForKey(stringRes.resourceId, null, null) | ||
if (stringInDefaultBundle == stringRes.resourceId) { | ||
val fallbackLocale = stringRes.bundle.developmentLocalization ?: FALLBACK_LOCALE | ||
val fallbackLocaleBundle = StringDesc.LocaleType.Custom(fallbackLocale).getLocaleBundle(bundle) | ||
fallbackLocaleBundle.localizedStringForKey(stringRes.resourceId, null, null) | ||
} else { | ||
stringInDefaultBundle | ||
} | ||
} else { | ||
stringInCurrentLocale | ||
} | ||
} |
This file contains 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