2018-11-08 19:45:21 +00:00
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
from django.test import TestCase, Client
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from peeringdb_server.models import Organization, User
|
|
|
|
|
from string import Formatter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocaleFilesTest(TestCase):
|
|
|
|
|
def load_messages(self, language, filename="django.po"):
|
|
|
|
|
path = os.path.join(
|
2019-12-05 16:57:52 +00:00
|
|
|
os.path.dirname(__file__), "..", "locale", language, "LC_MESSAGES"
|
|
|
|
|
)
|
2020-07-15 02:07:01 -05:00
|
|
|
with open(os.path.join(path, filename)) as fh:
|
2018-11-08 19:45:21 +00:00
|
|
|
content = fh.read()
|
|
|
|
|
message_id = re.findall(r"\nmsgid (.+)\n", content)
|
|
|
|
|
message_str = re.findall(r"\nmsgstr (.+)\n", content)
|
2020-01-08 13:29:58 -06:00
|
|
|
messages = dict(list(zip(message_id, message_str)))
|
2018-11-08 19:45:21 +00:00
|
|
|
return messages
|
|
|
|
|
|
2019-03-14 08:27:46 +00:00
|
|
|
# weblate handles all this now, and these tests are failing
|
|
|
|
|
# atm because the locale files no longer reside here
|
|
|
|
|
#
|
|
|
|
|
# weblate also makes sure that variable formatting matches, so this
|
|
|
|
|
# test is somewhat redundant at this point.
|
|
|
|
|
#
|
|
|
|
|
# either need to redo this test and make sure it generates the locale
|
|
|
|
|
# or remove it.
|
|
|
|
|
def _test_pt(self):
|
2018-11-08 19:45:21 +00:00
|
|
|
"""
|
|
|
|
|
Test portuguese locale files
|
|
|
|
|
"""
|
|
|
|
|
self.assert_variables(
|
2019-12-05 16:57:52 +00:00
|
|
|
self.load_messages("en_US"), self.load_messages("pt"), "PT"
|
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
self.assert_variables(
|
|
|
|
|
self.load_messages("en_US", filename="djangojs.po"),
|
2019-12-05 16:57:52 +00:00
|
|
|
self.load_messages("pt", filename="djangojs.po"),
|
|
|
|
|
"PT",
|
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
def assert_variables(self, en_messages, other_messages, language):
|
|
|
|
|
"""
|
|
|
|
|
Assert that the correct formatting variables exist
|
|
|
|
|
"""
|
|
|
|
|
errors = 0
|
|
|
|
|
|
2020-01-08 13:29:58 -06:00
|
|
|
for msgid, msgstr in list(en_messages.items()):
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
# %(name)s and %s type variables
|
2020-01-08 13:29:58 -06:00
|
|
|
variables_a = sorted(re.findall(r"%\([^\(]+\)s|%s", msgid))
|
|
|
|
|
variables_b = sorted(re.findall(r"%\([^\(]+\)s|%s", other_messages[msgid]))
|
2018-11-08 19:45:21 +00:00
|
|
|
if variables_a != variables_b:
|
|
|
|
|
errors += 1
|
2020-01-08 13:29:58 -06:00
|
|
|
print(
|
|
|
|
|
"{} Locale variable error at msgid {} -> {}".format(
|
|
|
|
|
language, msgid, other_messages[msgid]
|
|
|
|
|
)
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
# {name} and {} type variables
|
2019-12-05 16:57:52 +00:00
|
|
|
variables_a = sorted(
|
|
|
|
|
[fn for _, fn, _, _ in Formatter().parse(msgid) if fn is not None]
|
|
|
|
|
)
|
|
|
|
|
variables_b = [
|
|
|
|
|
fn
|
|
|
|
|
for _, fn, _, _ in Formatter().parse(other_messages[msgid])
|
2018-11-08 19:45:21 +00:00
|
|
|
if fn is not None
|
2019-12-05 16:57:52 +00:00
|
|
|
]
|
2018-11-08 19:45:21 +00:00
|
|
|
if variables_a != variables_b:
|
|
|
|
|
errors += 1
|
2020-01-08 13:29:58 -06:00
|
|
|
print(
|
|
|
|
|
"{} Locale variable error at msgid {} -> {}".format(
|
|
|
|
|
language, msgid, other_messages[msgid]
|
|
|
|
|
)
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
assert errors == 0
|