Commit defc52db authored by intrigeri's avatar intrigeri
Browse files

Support <LANG>_<SCRIPT>.po format.

i18nspector's --language option takes "a 2- or 3-letter ISO 639 language code,
possibly followed by underscore and a 2-letter ISO 3166 territory code"
so we need to strip any "_<SCRIPT>" suffix when passing it the language
we've detected from the PO file name. Otherwise, we get:

   i18nspector: error: invalid language
parent 27eafcb2
......@@ -106,6 +106,18 @@ class PoFile:
raise NoLanguageError(self.fname)
return m.group("lang")
def lang_without_script(self) -> str:
"""@returns: language of filename, with any script suffix stripped"""
lang = self.lang()
# a script suffix (e.g. "_Latn") starts with an underscore,
# followed by the script name in title case, which we
# approximate as an upper case letter followed by a lower case one
m = re.match(r"(?P<lang_without_script>.*)_[A-Z][a-z][A-Za-z]*$", lang)
if m:
return m.group("lang_without_script")
else:
return lang
def check(self, key: str, value: str) -> bool:
"""check if there is "key: value\\n" in PO header"""
try:
......@@ -161,7 +173,7 @@ class PoFile:
"""@returns a list of issues raised by i18nspector removes
allowed issues from @I18NINSPECTOR_ACCEPT.
"""
cmd = ["i18nspector", "--language", self.lang(), self.fname]
cmd = ["i18nspector", "--language", self.lang_without_script(), self.fname]
process = subprocess.run(
cmd,
stdout=subprocess.PIPE,
......
......@@ -112,6 +112,12 @@ class TestCheckPo(unittest.TestCase):
with self.assertRaises(lint_po.NoLanguageError, msg=_p.fname):
_p.lang()
def test_lang_without_script(self):
self.assertEqual(lint_po.PoFile("index.de.po").lang_without_script(), "de")
self.assertEqual(lint_po.PoFile("index.de_DE.po").lang_without_script(), "de_DE")
self.assertEqual(lint_po.PoFile("index.sr_Latn.po").lang_without_script(), "sr")
self.assertEqual(lint_po.PoFile("index.sr_LAtn.po").lang_without_script(), "sr_LAtn")
def test_needs_rewrap(self):
with lint_po.pofile_readonly(os.path.join(DIRNAME, "checkPo/length")) as poFile:
self.assertEqual(poFile.needs_rewrap(), True)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment