text: 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(
              os.path.dirname(__file__), "..", "locale", language, "LC_MESSAGES"
          )
          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(zip(message_id, message_str))
          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):
          """
          Test portuguese locale files
          """
          self.assert_variables(
              self.load_messages("en_US"), self.load_messages("pt"), "PT"
          )
          self.assert_variables(
              self.load_messages("en_US", filename="djangojs.po"),
              self.load_messages("pt", filename="djangojs.po"),
              "PT",
          )
  
      def assert_variables(self, en_messages, other_messages, language):
          """
          Assert that the correct formatting variables exist
          """
          errors = 0
  
          for msgid, msgstr in en_messages.items():
  
              # %(name)s and %s type variables
              variables_a = sorted(re.findall("%\([^\(]+\)s|%s", msgid))
              variables_b = sorted(re.findall("%\([^\(]+\)s|%s", other_messages[msgid]))
              if variables_a != variables_b:
                  errors += 1
                  print "{} Locale variable error at msgid {} -> {}".format(
                      language, msgid, other_messages[msgid]
                  )
  
              # {name} and {} type variables
              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])
                  if fn is not None
              ]
              if variables_a != variables_b:
                  errors += 1
                  print "{} Locale variable error at msgid {} -> {}".format(
                      language, msgid, other_messages[msgid]
                  )
  
          assert errors == 0
  