From 975b2ba165083ee8e0b894c1214c047d477bc40f Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Tue, 9 Jan 2024 11:42:04 +0100 Subject: [PATCH] Unit test against unmatched placeholders in a translation (#10863) --- tests/src/com/unciv/logic/TranslationTests.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/src/com/unciv/logic/TranslationTests.kt b/tests/src/com/unciv/logic/TranslationTests.kt index a77cc26b6b..9620835e7f 100644 --- a/tests/src/com/unciv/logic/TranslationTests.kt +++ b/tests/src/com/unciv/logic/TranslationTests.kt @@ -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