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
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ internal fun pluralizedString(
number: Int
): String {
val fallbackLocale = bundle.developmentLocalization ?: BASE_LOCALIZATION
val localized = bundle
.localizedStringForKey(resourceId, null, null)
.takeUnless { it == resourceId }
?: baseBundle.localizedStringForKey(resourceId, null, null)
.takeUnless { it == resourceId } ?: StringDesc.LocaleType.Custom(fallbackLocale)
.getLocaleBundle(bundle).localizedStringForKey(resourceId, null, null)
val localized = Utils.localizedStringOrNull(
bundle = bundle,
resourceId = resourceId
) ?: Utils.localizedStringOrNull(
bundle = baseBundle,
resourceId = resourceId
) ?: Utils.localizedStringOrNull(
bundle = StringDesc.LocaleType.Custom(fallbackLocale).getLocaleBundle(bundle),
resourceId = resourceId
) ?: resourceId
@Suppress("CAST_NEVER_SUCCEEDS")
return NSString.create(
format = localized,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,49 @@
package dev.icerock.moko.resources.desc

import dev.icerock.moko.resources.StringResource
import platform.Foundation.NSBundle
import platform.Foundation.NSString
import platform.Foundation.stringWithFormat

object Utils {
const val BASE_LOCALIZATION: String = "Base"
private const val MISSING_STRING_PREFIX: String = "__moko.resources.missing__"

fun processArgs(args: List<Any>): Array<out Any> {
return args.map { (it as? StringDesc)?.localized() ?: it }.toTypedArray()
}

fun localizedStringOrNull(bundle: NSBundle, resourceId: String): String? {
val missingMarker = "$MISSING_STRING_PREFIX$resourceId"
return bundle.localizedStringForKey(
key = resourceId,
value = missingMarker,
table = null
).takeUnless { it == missingMarker }
}

fun localizedString(stringRes: StringResource): String {
val bundle = StringDesc.localeType.getLocaleBundle(stringRes.bundle)
val stringInCurrentLocale = bundle.localizedStringForKey(
key = stringRes.resourceId,
value = null,
table = null
val stringInCurrentLocale = localizedStringOrNull(
bundle = bundle,
resourceId = stringRes.resourceId
)

return if (stringInCurrentLocale == stringRes.resourceId) {
val stringInDefaultBundle = stringRes.bundle.localizedStringForKey(
key = stringRes.resourceId,
value = null,
table = null
return if (stringInCurrentLocale == null) {
val stringInDefaultBundle = localizedStringOrNull(
bundle = stringRes.bundle,
resourceId = stringRes.resourceId
)

if (stringInDefaultBundle == stringRes.resourceId) {
if (stringInDefaultBundle == null) {
val fallbackLocale = stringRes.bundle.developmentLocalization ?: BASE_LOCALIZATION
val fallbackLocaleBundle = StringDesc.LocaleType
.Custom(fallbackLocale)
.getLocaleBundle(stringRes.bundle)
fallbackLocaleBundle.localizedStringForKey(
key = stringRes.resourceId,
value = null,
table = null
)
localizedStringOrNull(
bundle = fallbackLocaleBundle,
resourceId = stringRes.resourceId
) ?: stringRes.resourceId
} else {
stringInDefaultBundle
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ class AppleLocalizationBundleTests {
)
StringDesc.localeType = StringDesc.LocaleType.System
}

@Test
fun localizedStringSameAsKeyInLocalizedBundleTest() {
val resource = StringResource(
resourceId = "middag",
bundle = NSBundle.bundleWithPath(NSBundle.mainBundle.bundlePath + "/tests.bundle")!!
)
StringDesc.localeType = StringDesc.LocaleType.Custom("nl")
val stringDesc = ResourceStringDesc(resource)
assertEquals(
expected = "middag",
actual = stringDesc.localized()
)
StringDesc.localeType = StringDesc.LocaleType.System
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"noResultsFound" = "No results found";
"noInternetConnection" = "No internet connection";
"noInternetConnection" = "No internet connection";
"middag" = "afternoon";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"middag" = "middag";
Loading