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
23 changes: 20 additions & 3 deletions lib/src/main/kotlin/com/appswithlove/loco/util/TaskUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ object TaskUtils {
fetchAllLanguages(httpClient, locoConfig.apiKey).ifEmpty { emptyList() }
}

val defaultLangEntry = findDefaultLangEntry(languages, locoConfig.defLang)

for (langEntry in languages) {
var lang = langEntry
var text = httpClient.fetchTranslation(locoConfig, lang)
Expand Down Expand Up @@ -60,7 +62,9 @@ object TaskUtils {
text = text.replaceFirst(wrongXmlString, expectedXmlString)
}

if (lang == locoConfig.defLang) {
val isDefaultLang = langEntry == defaultLangEntry

if (isDefaultLang) {
if (locoConfig.ignoreMissingTranslationWarnings) {
text = text.replaceFirst(
"<resources",
Expand All @@ -70,7 +74,7 @@ object TaskUtils {
saveFile(locoConfig, text)
}

if (lang != locoConfig.defLang || locoConfig.saveDefLangDuplicate) {
if (!isDefaultLang || locoConfig.saveDefLangDuplicate) {
saveFile(locoConfig, text, appendix)
}
}
Expand Down Expand Up @@ -111,6 +115,7 @@ object TaskUtils {
fetchAllLanguages(httpClient, apiKey)
}

val defaultLangEntry = findDefaultLangEntry(locales, locoConfig.defLang)
val json = Json { ignoreUnknownKeys = true }

for (langEntry in locales) {
Expand All @@ -119,7 +124,7 @@ object TaskUtils {
} else {
langEntry
}
val folderSuffix = if (langEntry == locoConfig.defLang) "" else "-$androidLang"
val folderSuffix = if (langEntry == defaultLangEntry) "" else "-$androidLang"
val file = File("$resDir/values$folderSuffix/${locoConfig.fileName}.xml")

if (!file.exists()) {
Expand Down Expand Up @@ -150,6 +155,18 @@ object TaskUtils {
}
}

private fun findDefaultLangEntry(languages: List<String>, defLang: String?): String? {
if (defLang == null) return null
return languages.firstOrNull { entry ->
val converted = if (entry.contains("-")) {
entry.replace("-", "-r")
} else {
entry
}
entry == defLang || converted == defLang || entry.startsWith("$defLang-")
}
}

private fun saveFile(locoConfig: LocoConfig, text: String, appendix: String = "") {
val directory = File("${locoConfig.resDir}/values$appendix/")
if (!directory.exists()) {
Expand Down
100 changes: 97 additions & 3 deletions lib/src/test/kotlin/com/appswithlove/loco/util/TaskUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ class TaskUtilsTest {

TaskUtils.generate(config { ignoreMissingTranslationWarnings = true }, fake)

File(tempDir, "values/strings.xml").readText() shouldContain "tools:ignore=\"MissingTranslation\""
File(
tempDir,
"values/strings.xml"
).readText() shouldContain "tools:ignore=\"MissingTranslation\""
}

@Test
fun generate_withReplaceMap_replacesTextInOutput() {
fake.translationResponses["en"] = "<resources><string name=\"a\">Hello World</string></resources>"
fake.translationResponses["en"] =
"<resources><string name=\"a\">Hello World</string></resources>"

TaskUtils.generate(config { replace = mapOf("World" to "Earth") }, fake)

Expand All @@ -200,7 +204,10 @@ class TaskUtilsTest {
@Test
fun push_withValidConfig_pushesXmlForEachLocale() {
File(tempDir, "values").mkdirs()
File(tempDir, "values/strings.xml").writeText("<resources><string name=\"hello\">Hello</string></resources>")
File(
tempDir,
"values/strings.xml"
).writeText("<resources><string name=\"hello\">Hello</string></resources>")

TaskUtils.push(config(), fake)

Expand Down Expand Up @@ -289,6 +296,93 @@ class TaskUtilsTest {
}
}

@Test
fun generate_withCountrySuffixLangMatchingDefLang_savesFileInDefaultDirectory() {
fake.translationResponses["en-CA"] = "<resources></resources>"
fake.translationResponses["fr-CA"] = "<resources></resources>"

TaskUtils.generate(
locoConfig = config {
defLang = "en"
lang = listOf("en-CA", "fr-CA")
},
httpClient = fake,
)

File(tempDir, "values/strings.xml").exists() shouldBe true
File(tempDir, "values-en-rCA/strings.xml").exists() shouldBe false
File(tempDir, "values-fr-rCA/strings.xml").exists() shouldBe true
}

@Test
fun generate_withCountrySuffixLangAndSaveDefLangDuplicate_savesFilesInBothDirectories() {
fake.translationResponses["en-CA"] = "<resources></resources>"

TaskUtils.generate(
locoConfig = config {
defLang = "en"
lang = listOf("en-CA")
saveDefLangDuplicate = true
},
httpClient = fake,
)

File(tempDir, "values/strings.xml").exists() shouldBe true
File(tempDir, "values-en-rCA/strings.xml").exists() shouldBe true
}

@Test
fun generate_withMultipleSameBaseLang_onlyFirstSavesToDefaultDirectory() {
fake.translationResponses["en-CA"] =
"<resources><string name=\"a\">CA</string></resources>"
fake.translationResponses["en-US"] =
"<resources><string name=\"a\">US</string></resources>"

TaskUtils.generate(
locoConfig = config {
defLang = "en"
lang = listOf("en-CA", "en-US")
},
httpClient = fake,
)

File(tempDir, "values/strings.xml").exists() shouldBe true
File(tempDir, "values/strings.xml").readText() shouldContain "CA"
File(tempDir, "values-en-rUS/strings.xml").exists() shouldBe true
}

@Test
fun generate_withAndroidStyleDefLangWorkaround_stillSavesToDefaultDirectory() {
fake.translationResponses["en-CA"] = "<resources></resources>"

// Existing workaround: users set defLang to Android-style "en-rCA"
TaskUtils.generate(
locoConfig = config {
defLang = "en-rCA"
lang = listOf("en-CA")
},
httpClient = fake,
)

File(tempDir, "values/strings.xml").exists() shouldBe true
}

@Test
fun push_withCountrySuffixLangMatchingDefLang_readsFromDefaultDirectory() {
File(tempDir, "values").mkdirs()
File(tempDir, "values/strings.xml").writeText("<resources></resources>")

TaskUtils.push(
locoConfig = config {
defLang = "en"
lang = listOf("en-CA")
},
httpClient = fake,
)

fake.capturedPushLocales shouldBe listOf("en-CA")
}

@Test
fun generate_withNullApiKey_throwsGradleExceptionListingSources() {
val exception = shouldThrow<GradleException> {
Expand Down
Loading