|
5 | 5 | package dev.icerock.moko.resources.desc |
6 | 6 |
|
7 | 7 | import dev.icerock.moko.resources.StringResource |
| 8 | +import platform.Foundation.NSBundle |
8 | 9 | import platform.Foundation.NSString |
9 | 10 | import platform.Foundation.stringWithFormat |
10 | 11 |
|
11 | 12 | object Utils { |
12 | 13 | const val BASE_LOCALIZATION: String = "Base" |
| 14 | + private const val MISSING_STRING_PREFIX: String = "__moko.resources.missing__" |
13 | 15 |
|
14 | 16 | fun processArgs(args: List<Any>): Array<out Any> { |
15 | 17 | return args.map { (it as? StringDesc)?.localized() ?: it }.toTypedArray() |
16 | 18 | } |
17 | 19 |
|
| 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 | + |
18 | 29 | fun localizedString(stringRes: StringResource): String { |
19 | 30 | 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 |
24 | 34 | ) |
25 | 35 |
|
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 |
31 | 40 | ) |
32 | 41 |
|
33 | | - if (stringInDefaultBundle == stringRes.resourceId) { |
| 42 | + if (stringInDefaultBundle == null) { |
34 | 43 | val fallbackLocale = stringRes.bundle.developmentLocalization ?: BASE_LOCALIZATION |
35 | 44 | val fallbackLocaleBundle = StringDesc.LocaleType |
36 | 45 | .Custom(fallbackLocale) |
37 | 46 | .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 |
43 | 51 | } else { |
44 | 52 | stringInDefaultBundle |
45 | 53 | } |
|
0 commit comments