Skip to content

Commit

Permalink
Merge pull request #470 from colintheshots-meetup/develop
Browse files Browse the repository at this point in the history
Fix #118: Fill in missing string translations with base translation
  • Loading branch information
Alex009 authored Jun 8, 2024
2 parents db8aa57 + fab0b79 commit d0f30dd
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.icerock.moko.resources.desc

import dev.icerock.moko.resources.PluralsResource
import dev.icerock.moko.resources.desc.Utils.BASE_LOCALIZATION
import kotlinx.cinterop.BetaInteropApi
import platform.Foundation.NSBundle
import platform.Foundation.NSLocale
Expand Down Expand Up @@ -39,14 +40,18 @@ internal fun pluralizedString(
resourceId: String,
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)
@Suppress("CAST_NEVER_SUCCEEDS")
return NSString.create(
format = localized,
locale = locale,
args = arrayOf(number)
) as String
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,43 @@ import platform.Foundation.NSString
import platform.Foundation.stringWithFormat

object Utils {
const val BASE_LOCALIZATION: String = "Base"

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

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

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

if (stringInDefaultBundle == stringRes.resourceId) {
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
)
} else {
stringInDefaultBundle
}
} else {
stringInCurrentLocale
}
}

fun stringWithFormat(format: String, args: Array<out Any>): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import dev.icerock.moko.resources.StringResource
import platform.Foundation.NSBundle
import kotlin.test.Test
import kotlin.test.assertEquals

class AppleLocalizationBundleTests {

@Test
fun localizedStringWithLocalizationCaseTest() {
val resource = StringResource(
resourceId = "noResultsFound",
bundle = NSBundle.bundleWithPath(NSBundle.mainBundle.bundlePath + "/tests.bundle")!!
)
StringDesc.localeType = StringDesc.LocaleType.Custom("es-US")
val stringDesc = ResourceStringDesc(
resource
)
assertEquals(
expected = "No se han encontrado resultados",
actual = stringDesc.localized()
)
StringDesc.localeType = StringDesc.LocaleType.System
}

@Test
fun localizedStringMissingLocalizationCaseTest() {
val resource = StringResource(
resourceId = "noInternetConnection",
bundle = NSBundle.bundleWithPath(NSBundle.mainBundle.bundlePath + "/tests.bundle")!!
)
StringDesc.localeType = StringDesc.LocaleType.Custom("es-US")
val stringDesc = ResourceStringDesc(
resource
)
assertEquals(
expected = "No internet connection",
actual = stringDesc.localized()
)
StringDesc.localeType = StringDesc.LocaleType.System
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class PluralFormattedStringDescStringTests {
)
}

@Test
fun testMissingLocalizationCase() {
StringDesc.localeType = StringDesc.LocaleType.Custom("es-US")
assertEquals(
expected = "6/10 items",
actual = createPluralFormatted(6).localized()
)
StringDesc.localeType = StringDesc.LocaleType.System
}

private fun createPluralFormatted(number: Int): PluralFormattedStringDesc {
val pluralResource = PluralsResource(
resourceId = "stringFormatted",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"noResultsFound" = "No results found";
"noInternetConnection" = "No internet connection";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"noResultsFound" = "No se han encontrado resultados";
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dev.icerock.moko.resources.getImageByFileName
public object Testing {
public fun getStrings(): List<StringDesc> {
return listOf(
MR.strings.text_only_in_base.desc(),
MR.strings.test_simple.desc(),
MR.strings.test2.desc(),
MR.strings.test3.desc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="multilined">first line\nsecond line\nthird line.</string>
<string name="quotes">Alex009 said "hello world" &amp; "write tests".</string>
<string name="single_quotes">Alex009 said 'hello'</string>
<string name="text_only_in_base">Text from base locale</string>
</resources>

0 comments on commit d0f30dd

Please sign in to comment.