Skip to content

Commit bd6c032

Browse files
authored
Merge pull request #873 from icerockdev/develop
2 parents d418edb + 5690bbe commit bd6c032

8 files changed

Lines changed: 61 additions & 28 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ buildscript {
6565
}
6666
6767
dependencies {
68-
classpath "dev.icerock.moko:resources-generator:0.26.2"
68+
classpath "dev.icerock.moko:resources-generator:0.26.3"
6969
}
7070
}
7171
@@ -83,10 +83,10 @@ project build.gradle
8383
apply plugin: "dev.icerock.mobile.multiplatform-resources"
8484
8585
dependencies {
86-
commonMainApi("dev.icerock.moko:resources:0.26.2")
87-
commonMainApi("dev.icerock.moko:resources-compose:0.26.2") // for compose multiplatform
86+
commonMainApi("dev.icerock.moko:resources:0.26.3")
87+
commonMainApi("dev.icerock.moko:resources-compose:0.26.3") // for compose multiplatform
8888
89-
commonTestImplementation("dev.icerock.moko:resources-test:0.26.2")
89+
commonTestImplementation("dev.icerock.moko:resources-test:0.26.3")
9090
}
9191
9292
multiplatformResources {
@@ -133,7 +133,7 @@ should [add `export` declarations](https://kotlinlang.org/docs/multiplatform-bui
133133

134134
```
135135
framework {
136-
export("dev.icerock.moko:resources:0.26.2")
136+
export("dev.icerock.moko:resources:0.26.3")
137137
export("dev.icerock.moko:graphics:0.10.0") // toUIColor here
138138
}
139139
```

gradle/moko.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
resourcesVersion = "0.26.2"
2+
resourcesVersion = "0.26.3"
33

44
[libraries]
55
resources = { module = "dev.icerock.moko:resources", version.ref = "resourcesVersion" }

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";

samples/default-hierarchy-gallery-mobile/mpp-library/mpp_library.podspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Pod::Spec.new do |spec|
2222
Alternatively, proper pod installation is performed during Gradle sync in the IDE (if Podfile location is set)"
2323
end
2424

25+
spec.xcconfig = {
26+
'ENABLE_USER_SCRIPT_SANDBOXING' => 'NO',
27+
}
28+
2529
spec.pod_target_xcconfig = {
2630
'KOTLIN_PROJECT_PATH' => ':mpp-library',
2731
'PRODUCT_MODULE_NAME' => 'MultiPlatformLibrary',

0 commit comments

Comments
 (0)