<commit_before>import gettext
import subprocess
import sys
import unittest
import os
import shutil

sys.path.insert(0, ".")
if sys.version_info < (3, 4):
    import imp as importlib
else:
    import importlib

from coalib.misc import i18n


def set_lang(lang):
    os.environ["LANGUAGE"] = lang
    os.environ["LC_ALL"] = lang
    os.environ["LC_MESSAGES"] = lang
    os.environ["LANG"] = lang
    gettext._default_localedir = os.path.abspath(os.path.join("build",
                                                              "locale"))
    importlib.reload(i18n)


class i18nTest(unittest.TestCase):
    def test_de(self):
        set_lang("de_DE.UTF8")
        # Do not change this translation without changing it in the code also!
        self.assertEqual(i18n._("A string to test translations."),
                         "Eine Zeichenkette um Übersetzungen zu testen.")

    def test_unknown(self):
        set_lang("unknown_language")
        self.assertEqual(i18n._("A string to test translations."),
                         "A string to test translations.")

    def test_translation_marking(self):
        set_lang("de_DE.UTF8")
        string = "A not directly translated test string."
        self.assertEqual(i18n.N_("A not directly translated test string."),
                         string)
        self.assertEqual(i18n._(string),
                         "Ein indirekt übersetzter test String.")


def skip_test():
    try:
        subprocess.Popen(['msgfmt'],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        return False
    except OSError:
        return "msgfmt is not installed."


if __name__ == '__main__':
    build_dir = "build-i18n"
    gettext._default_localedir = os.path.abspath(os.path.join(build_dir,
                                                              "locale"))
    importlib.reload(i18n)
    print("Testing translation building...")
    shutil.rmtree(build_dir, ignore_errors=True)

    i18n.compile_translations(build_dir)
    # Shouldn't complain if files are already there, ideally not rebuild!
    i18n.compile_translations(build_dir)
    shutil.rmtree(build_dir, ignore_errors=True)

    unittest.main(verbosity=2)<commit_msg>i18nTest: Move test_compilation into own test<commit_after>import gettext
import subprocess
import sys
import unittest
import os
import shutil

sys.path.insert(0, ".")
if sys.version_info < (3, 4):
    import imp as importlib
else:
    import importlib

from coalib.misc import i18n


def set_lang(lang):
    os.environ["LANGUAGE"] = lang
    os.environ["LC_ALL"] = lang
    os.environ["LC_MESSAGES"] = lang
    os.environ["LANG"] = lang
    gettext._default_localedir = os.path.abspath(os.path.join("build",
                                                              "locale"))
    importlib.reload(i18n)


class i18nTest(unittest.TestCase):
    def test_compilation(self):
        build_dir = "build-i18n"
        gettext._default_localedir = os.path.abspath(os.path.join(build_dir,
                                                                  "locale"))
        importlib.reload(i18n)
        shutil.rmtree(build_dir, ignore_errors=True)

        i18n.compile_translations(build_dir)
        self.assertTrue(os.path.exists(gettext._default_localedir))
        # Shouldn't complain if files are already there, ideally not rebuild!
        i18n.compile_translations(build_dir)
        shutil.rmtree(build_dir, ignore_errors=True)

    def test_de(self):
        set_lang("de_DE.UTF8")
        # Do not change this translation without changing it in the code also!
        self.assertEqual(i18n._("A string to test translations."),
                         "Eine Zeichenkette um Übersetzungen zu testen.")

    def test_unknown(self):
        set_lang("unknown_language")
        self.assertEqual(i18n._("A string to test translations."),
                         "A string to test translations.")

    def test_translation_marking(self):
        set_lang("de_DE.UTF8")
        string = "A not directly translated test string."
        self.assertEqual(i18n.N_("A not directly translated test string."),
                         string)
        self.assertEqual(i18n._(string),
                         "Ein indirekt übersetzter test String.")


def skip_test():
    try:
        subprocess.Popen(['msgfmt'],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        return False
    except OSError:
        return "msgfmt is not installed."


if __name__ == '__main__':
    unittest.main(verbosity=2)
{'fraction_non_alphanumeric': 0.09692877368764975, 'fraction_numerical': 0.01263341319973862, 'mean_word_length': 2.208944793850454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335492', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1320, 'n_words': 304}