1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_locale_files.py

79 lines
2.7 KiB
Python
Raw Normal View History

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"
)
2018-11-08 19:45:21 +00:00
with open(os.path.join(path, filename), "r") as fh:
content = fh.read()
message_id = re.findall(r"\nmsgid (.+)\n", content)
message_str = re.findall(r"\nmsgstr (.+)\n", content)
messages = dict(list(zip(message_id, message_str)))
2018-11-08 19:45:21 +00:00
return messages
# 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
for msgid, msgstr in list(en_messages.items()):
2018-11-08 19:45:21 +00:00
# %(name)s and %s type variables
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
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
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