def do_test(config, create_app): config.SUPPORTED_LOCALES = ['en_US', 'fr_FR'] config.TRANSLATION_DIRS = Path(config.TEMP_DIR) i18n_tool.I18NTool().main([ '--verbose', 'translate-messages', '--mapping', os.path.join(TESTS_DIR, 'i18n/babel.cfg'), '--translations-dir', config.TEMP_DIR, '--sources', os.path.join(TESTS_DIR, 'i18n/code.py'), '--extract-update', '--compile', ]) for l in ('en_US', 'fr_FR'): pot = os.path.join(config.TEMP_DIR, 'messages.pot') pybabel('init', '-i', pot, '-d', config.TEMP_DIR, '-l', l) app = create_app(config) with app.app_context(): db.create_all() assert list(i18n.LOCALES.keys()) == config.SUPPORTED_LOCALES verify_filesizeformat(app) verify_rel_datetime_format(app)
def test_translate_messages_l10n(self, tmpdir): source = [ join(self.dir, 'i18n/code.py'), join(self.dir, 'i18n/template.html'), ] args = [ '--verbose', 'translate-messages', '--translations-dir', str(tmpdir), '--mapping', join(self.dir, 'i18n/babel.cfg'), '--sources', ",".join(source), '--extract-update', '--compile', ] i18n_tool.I18NTool().main(args) messages_file = join(str(tmpdir), 'messages.pot') assert exists(messages_file) with io.open(messages_file, 'rb') as fobj: pot = fobj.read() assert b'code hello i18n' in pot assert b'template hello i18n' in pot locale = 'en_US' locale_dir = join(str(tmpdir), locale) pybabel('init', '-i', messages_file, '-d', str(tmpdir), '-l', locale) mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo') assert not exists(mo_file) i18n_tool.I18NTool().main(args) assert exists(mo_file) with io.open(mo_file, mode='rb') as fobj: mo = fobj.read() assert b'code hello i18n' in mo assert b'template hello i18n' in mo
def do_test(config, create_app): config.SUPPORTED_LOCALES = ["en_US", "fr_FR"] config.TRANSLATION_DIRS = Path(config.TEMP_DIR) i18n_tool.I18NTool().main([ "--verbose", "translate-messages", "--mapping", os.path.join(TESTS_DIR, "i18n/babel.cfg"), "--translations-dir", config.TEMP_DIR, "--sources", os.path.join(TESTS_DIR, "i18n/code.py"), "--extract-update", "--compile", ]) for l in ("en_US", "fr_FR"): pot = os.path.join(config.TEMP_DIR, "messages.pot") pybabel("init", "-i", pot, "-d", config.TEMP_DIR, "-l", l) app = create_app(config) with app.app_context(): db.create_all() assert list(i18n.LOCALES.keys()) == config.SUPPORTED_LOCALES verify_filesizeformat(app) verify_rel_datetime_format(app)
def test_translate_messages_compile_arg(self, tmpdir): args = [ '--verbose', 'translate-messages', '--translations-dir', str(tmpdir), '--mapping', join(self.dir, 'i18n/babel.cfg'), ] i18n_tool.I18NTool().main(args + [ '--sources', join(self.dir, 'i18n/code.py'), '--extract-update', ]) messages_file = join(str(tmpdir), 'messages.pot') assert exists(messages_file) with io.open(messages_file) as fobj: pot = fobj.read() assert 'code hello i18n' in pot locale = 'en_US' locale_dir = join(str(tmpdir), locale) po_file = join(locale_dir, 'LC_MESSAGES/messages.po') pybabel(['init', '-i', messages_file, '-d', str(tmpdir), '-l', locale]) assert exists(po_file) # pretend this happened a few seconds ago few_seconds_ago = time.time() - 60 os.utime(po_file, (few_seconds_ago, few_seconds_ago)) mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo') # # Extract+update but do not compile # old_po_mtime = getmtime(po_file) assert not exists(mo_file) i18n_tool.I18NTool().main(args + [ '--sources', join(self.dir, 'i18n/code.py'), '--extract-update', ]) assert not exists(mo_file) current_po_mtime = getmtime(po_file) assert old_po_mtime < current_po_mtime # # Compile but do not extract+update # source = [ join(self.dir, 'i18n/code.py'), join(self.dir, 'i18n/template.html'), ] old_po_mtime = current_po_mtime i18n_tool.I18NTool().main(args + [ '--sources', ",".join(source), '--compile', ]) assert old_po_mtime == getmtime(po_file) with io.open(mo_file, mode='rb') as fobj: mo = fobj.read() assert b'code hello i18n' in mo assert b'template hello i18n' not in mo
def test_i18n(journalist_app, config): # Then delete it because using it won't test what we want del journalist_app sources = [ os.path.join(TESTS_DIR, "i18n/code.py"), os.path.join(TESTS_DIR, "i18n/template.html"), ] i18n_tool.I18NTool().main([ "--verbose", "translate-messages", "--mapping", os.path.join(TESTS_DIR, "i18n/babel.cfg"), "--translations-dir", config.TEMP_DIR, "--sources", ",".join(sources), "--extract-update", ]) pot = os.path.join(config.TEMP_DIR, "messages.pot") pybabel("init", "-i", pot, "-d", config.TEMP_DIR, "-l", "en_US") for (l, s) in ( ("fr_FR", "code bonjour"), ("zh_Hans", "code chinese"), ("ar", "code arabic"), ("nb_NO", "code norwegian"), ("es_ES", "code spanish"), ): pybabel("init", "-i", pot, "-d", config.TEMP_DIR, "-l", l) po = os.path.join(config.TEMP_DIR, l, "LC_MESSAGES/messages.po") sed("-i", "-e", '/code hello i18n/,+1s/msgstr ""/msgstr "{}"/'.format(s), po) i18n_tool.I18NTool().main([ "--verbose", "translate-messages", "--translations-dir", config.TEMP_DIR, "--compile", ]) fake_config = SDConfig() fake_config.SUPPORTED_LOCALES = [ "ar", "en_US", "fr_FR", "nb_NO", "zh_Hans" ] fake_config.TRANSLATION_DIRS = Path(config.TEMP_DIR) # Use our config (and not an app fixture) because the i18n module # grabs values at init time and we can't inject them later. for app in (journalist_app_module.create_app(fake_config), source_app.create_app(fake_config)): with app.app_context(): db.create_all() assert list(i18n.LOCALES.keys()) == fake_config.SUPPORTED_LOCALES verify_i18n(app)
def test_i18n(journalist_app, config): # Then delete it because using it won't test what we want del journalist_app sources = [ os.path.join(TESTS_DIR, 'i18n/code.py'), os.path.join(TESTS_DIR, 'i18n/template.html'), ] i18n_tool.I18NTool().main([ '--verbose', 'translate-messages', '--mapping', os.path.join(TESTS_DIR, 'i18n/babel.cfg'), '--translations-dir', config.TEMP_DIR, '--sources', ",".join(sources), '--extract-update', ]) pot = os.path.join(config.TEMP_DIR, 'messages.pot') pybabel('init', '-i', pot, '-d', config.TEMP_DIR, '-l', 'en_US') for (l, s) in (('fr_FR', 'code bonjour'), ('zh_Hans_CN', 'code chinese'), ('ar', 'code arabic'), ('nb_NO', 'code norwegian'), ('es_ES', 'code spanish')): pybabel('init', '-i', pot, '-d', config.TEMP_DIR, '-l', l) po = os.path.join(config.TEMP_DIR, l, 'LC_MESSAGES/messages.po') sed('-i', '-e', '/code hello i18n/,+1s/msgstr ""/msgstr "{}"/'.format(s), po) i18n_tool.I18NTool().main([ '--verbose', 'translate-messages', '--translations-dir', config.TEMP_DIR, '--compile', ]) fake_config = SDConfig() fake_config.SUPPORTED_LOCALES = [ 'en_US', 'fr_FR', 'zh_Hans_CN', 'ar', 'nb_NO' ] fake_config.TRANSLATION_DIRS = config.TEMP_DIR # Use our config (and not an app fixture) because the i18n module # grabs values at init time and we can't inject them later. for app in (journalist_app_module.create_app(fake_config), source_app.create_app(fake_config)): with app.app_context(): db.create_all() assert i18n.LOCALES == fake_config.SUPPORTED_LOCALES verify_i18n(app)
def test_translate_messages_l10n(self, tmpdir): source = [ join(self.dir, "i18n/code.py"), join(self.dir, "i18n/template.html"), ] args = [ "--verbose", "translate-messages", "--translations-dir", str(tmpdir), "--mapping", join(self.dir, "i18n/babel.cfg"), "--sources", ",".join(source), "--extract-update", "--compile", ] i18n_tool.I18NTool().main(args) messages_file = join(str(tmpdir), "messages.pot") assert exists(messages_file) with io.open(messages_file, "rb") as fobj: pot = fobj.read() assert b"code hello i18n" in pot assert b"template hello i18n" in pot locale = "en_US" locale_dir = join(str(tmpdir), locale) pybabel("init", "-i", messages_file, "-d", str(tmpdir), "-l", locale) po_file = join(locale_dir, "LC_MESSAGES/messages.po") dummy_translate(po_file) mo_file = join(locale_dir, "LC_MESSAGES/messages.mo") assert not exists(mo_file) i18n_tool.I18NTool().main(args) assert exists(mo_file) with io.open(mo_file, mode="rb") as fobj: mo = fobj.read() assert b"code hello i18n" in mo assert b"template hello i18n" in mo
def test_translate_messages_compile_arg(self, tmpdir): args = [ "--verbose", "translate-messages", "--translations-dir", str(tmpdir), "--mapping", join(self.dir, "i18n/babel.cfg"), ] i18n_tool.I18NTool().main(args + [ "--sources", join(self.dir, "i18n/code.py"), "--extract-update", ]) messages_file = join(str(tmpdir), "messages.pot") assert exists(messages_file) with io.open(messages_file) as fobj: pot = fobj.read() assert "code hello i18n" in pot locale = "en_US" locale_dir = join(str(tmpdir), locale) po_file = join(locale_dir, "LC_MESSAGES/messages.po") pybabel(["init", "-i", messages_file, "-d", str(tmpdir), "-l", locale]) assert exists(po_file) # pretend this happened a few seconds ago few_seconds_ago = time.time() - 60 os.utime(po_file, (few_seconds_ago, few_seconds_ago)) mo_file = join(locale_dir, "LC_MESSAGES/messages.mo") # # Extract+update but do not compile # old_po_mtime = getmtime(po_file) assert not exists(mo_file) i18n_tool.I18NTool().main(args + [ "--sources", join(self.dir, "i18n/code.py"), "--extract-update", ]) assert not exists(mo_file) current_po_mtime = getmtime(po_file) assert old_po_mtime < current_po_mtime # # Translation would occur here - let's fake it dummy_translate(po_file) # # Compile but do not extract+update # source = [ join(self.dir, "i18n/code.py"), join(self.dir, "i18n/template.html"), ] current_po_mtime = getmtime(po_file) i18n_tool.I18NTool().main(args + [ "--sources", ",".join(source), "--compile", ]) assert current_po_mtime == getmtime(po_file) with io.open(mo_file, mode="rb") as fobj: mo = fobj.read() assert b"code hello i18n" in mo assert b"template hello i18n" not in mo