Skip to content

Commit 197d71b

Browse files
authored
Merge pull request #872 from SubhrajyotiSen/fix/same-key-value
Fix missing translation if the key and value are the same
2 parents 579b67a + c5a194b commit 197d71b

5 files changed

Lines changed: 51 additions & 22 deletions

File tree

resources/src/appleMain/kotlin/dev/icerock/moko/resources/desc/PluralFormattedStringDesc.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ internal fun pluralizedString(
4141
number: Int
4242
): String {
4343
val fallbackLocale = bundle.developmentLocalization ?: BASE_LOCALIZATION
44-
val localized = bundle
45-
.localizedStringForKey(resourceId, null, null)
46-
.takeUnless { it == resourceId }
47-
?: baseBundle.localizedStringForKey(resourceId, null, null)
48-
.takeUnless { it == resourceId } ?: StringDesc.LocaleType.Custom(fallbackLocale)
49-
.getLocaleBundle(bundle).localizedStringForKey(resourceId, null, null)
44+
val localized = Utils.localizedStringOrNull(
45+
bundle = bundle,
46+
resourceId = resourceId
47+
) ?: Utils.localizedStringOrNull(
48+
bundle = baseBundle,
49+
resourceId = resourceId
50+
) ?: Utils.localizedStringOrNull(
51+
bundle = StringDesc.LocaleType.Custom(fallbackLocale).getLocaleBundle(bundle),
52+
resourceId = resourceId
53+
) ?: resourceId
5054
@Suppress("CAST_NEVER_SUCCEEDS")
5155
return NSString.create(
5256
format = localized,

resources/src/appleMain/kotlin/dev/icerock/moko/resources/desc/Utils.kt

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,49 @@
55
package dev.icerock.moko.resources.desc
66

77
import dev.icerock.moko.resources.StringResource
8+
import platform.Foundation.NSBundle
89
import platform.Foundation.NSString
910
import platform.Foundation.stringWithFormat
1011

1112
object Utils {
1213
const val BASE_LOCALIZATION: String = "Base"
14+
private const val MISSING_STRING_PREFIX: String = "__moko.resources.missing__"
1315

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

20+
fun localizedStringOrNull(bundle: NSBundle, resourceId: String): String? {
21+
val missingMarker = "$MISSING_STRING_PREFIX$resourceId"
22+
return bundle.localizedStringForKey(
23+
key = resourceId,
24+
value = missingMarker,
25+
table = null
26+
).takeUnless { it == missingMarker }
27+
}
28+
1829
fun localizedString(stringRes: StringResource): String {
1930
val bundle = StringDesc.localeType.getLocaleBundle(stringRes.bundle)
20-
val stringInCurrentLocale = bundle.localizedStringForKey(
21-
key = stringRes.resourceId,
22-
value = null,
23-
table = null
31+
val stringInCurrentLocale = localizedStringOrNull(
32+
bundle = bundle,
33+
resourceId = stringRes.resourceId
2434
)
2535

26-
return if (stringInCurrentLocale == stringRes.resourceId) {
27-
val stringInDefaultBundle = stringRes.bundle.localizedStringForKey(
28-
key = stringRes.resourceId,
29-
value = null,
30-
table = null
36+
return if (stringInCurrentLocale == null) {
37+
val stringInDefaultBundle = localizedStringOrNull(
38+
bundle = stringRes.bundle,
39+
resourceId = stringRes.resourceId
3140
)
3241

33-
if (stringInDefaultBundle == stringRes.resourceId) {
42+
if (stringInDefaultBundle == null) {
3443
val fallbackLocale = stringRes.bundle.developmentLocalization ?: BASE_LOCALIZATION
3544
val fallbackLocaleBundle = StringDesc.LocaleType
3645
.Custom(fallbackLocale)
3746
.getLocaleBundle(stringRes.bundle)
38-
fallbackLocaleBundle.localizedStringForKey(
39-
key = stringRes.resourceId,
40-
value = null,
41-
table = null
42-
)
47+
localizedStringOrNull(
48+
bundle = fallbackLocaleBundle,
49+
resourceId = stringRes.resourceId
50+
) ?: stringRes.resourceId
4351
} else {
4452
stringInDefaultBundle
4553
}

resources/src/iosTest/kotlin/dev/icerock/moko/resources/desc/AppleLocalizationBundleTests.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@ class AppleLocalizationBundleTests {
4444
)
4545
StringDesc.localeType = StringDesc.LocaleType.System
4646
}
47+
48+
@Test
49+
fun localizedStringSameAsKeyInLocalizedBundleTest() {
50+
val resource = StringResource(
51+
resourceId = "middag",
52+
bundle = NSBundle.bundleWithPath(NSBundle.mainBundle.bundlePath + "/tests.bundle")!!
53+
)
54+
StringDesc.localeType = StringDesc.LocaleType.Custom("nl")
55+
val stringDesc = ResourceStringDesc(resource)
56+
assertEquals(
57+
expected = "middag",
58+
actual = stringDesc.localized()
59+
)
60+
StringDesc.localeType = StringDesc.LocaleType.System
61+
}
4762
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
"noResultsFound" = "No results found";
2-
"noInternetConnection" = "No internet connection";
2+
"noInternetConnection" = "No internet connection";
3+
"middag" = "afternoon";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"middag" = "middag";

0 commit comments

Comments
 (0)