Unit test against unmatched placeholders in a translation (#10863)

This commit is contained in:
SomeTroglodyte 2024-01-09 11:42:04 +01:00 committed by GitHub
parent 6b469cb25b
commit 975b2ba165
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -117,6 +117,33 @@ class TranslationTests {
)
}
/** For every translatable string and all translations check if all translated placeholders are present in the key */
@Test
fun allTranslationsHaveNoExtraPlaceholders() {
var allTranslationsHaveNoExtraPlaceholders = true
val languages = translations.getLanguages()
for ((key, translation) in translations) {
val translationEntry = translation.entry
val placeholders = squareBraceRegex.findAll(translationEntry)
.map { it.value }.toSet()
for (language in languages) {
val output = translations.getText(key, language)
if (output == key) continue // the language doesn't have the required translation, so we got back the key
val translatedPlaceholders = squareBraceRegex.findAll(output)
.map { it.value }.toSet()
val extras = translatedPlaceholders - placeholders
for (placeholder in extras) {
allTranslationsHaveNoExtraPlaceholders = false
println("Extra placeholder `$placeholder` in `$language` for entry `$translationEntry`")
}
}
}
Assert.assertTrue(
"This test will only pass when all placeholders in all translations are present in the key",
allTranslationsHaveNoExtraPlaceholders
)
}
@Test
fun allPlaceholderKeysMatchEntry() {
var allPlaceholderKeysMatchEntry = true