Multilang: check format arguments are consistent (#29441)

* format args

* global
pull/25765/head
Shane Smiskol 2 years ago committed by GitHub
parent 549a4e9a97
commit 103d39259f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      selfdrive/ui/tests/test_translations.py

@ -11,6 +11,7 @@ from selfdrive.ui.update_translations import TRANSLATIONS_DIR, LANGUAGES_FILE, u
TMP_TRANSLATIONS_DIR = os.path.join(TRANSLATIONS_DIR, "tmp") TMP_TRANSLATIONS_DIR = os.path.join(TRANSLATIONS_DIR, "tmp")
UNFINISHED_TRANSLATION_TAG = "<translation type=\"unfinished\"" # non-empty translations can be marked unfinished UNFINISHED_TRANSLATION_TAG = "<translation type=\"unfinished\"" # non-empty translations can be marked unfinished
LOCATION_TAG = "<location " LOCATION_TAG = "<location "
FORMAT_ARG = re.compile("%[0-9]+")
class TestTranslations(unittest.TestCase): class TestTranslations(unittest.TestCase):
@ -75,6 +76,7 @@ class TestTranslations(unittest.TestCase):
- that the correct format specifier is used (%n) - that the correct format specifier is used (%n)
Non-plural: Non-plural:
- that translation is not empty - that translation is not empty
- that translation format arguments are consistent
""" """
for name, file in self.translation_files.items(): for name, file in self.translation_files.items():
with self.subTest(name=name, file=file): with self.subTest(name=name, file=file):
@ -95,11 +97,16 @@ class TestTranslations(unittest.TestCase):
for nf in numerusform: for nf in numerusform:
self.assertIsNotNone(nf, f"Ensure all plural translation forms are completed: {source_text}") self.assertIsNotNone(nf, f"Ensure all plural translation forms are completed: {source_text}")
self.assertIn("%n", nf, "Ensure numerus argument (%n) exists in translation.") self.assertIn("%n", nf, "Ensure numerus argument (%n) exists in translation.")
self.assertIsNone(re.search("%[0-9]+", nf), "Plural translations must use %n, not %1, %2, etc.: {}".format(numerusform)) self.assertIsNone(FORMAT_ARG.search(nf), "Plural translations must use %n, not %1, %2, etc.: {}".format(numerusform))
else: else:
self.assertIsNotNone(translation.text, f"Ensure translation is completed: {source_text}") self.assertIsNotNone(translation.text, f"Ensure translation is completed: {source_text}")
source_args = FORMAT_ARG.findall(source_text)
translation_args = FORMAT_ARG.findall(translation.text)
self.assertEqual(sorted(source_args), sorted(translation_args),
f"Ensure format arguments are consistent: `{source_text}` vs. `{translation.text}`")
def test_no_locations(self): def test_no_locations(self):
for name, file in self.translation_files.items(): for name, file in self.translation_files.items():
with self.subTest(name=name, file=file): with self.subTest(name=name, file=file):

Loading…
Cancel
Save