def test_none_exist(self, codecs_open, isfile): data = {} isfile.side_effect = lambda p: p in data codecs_open.side_effect = lambda p, _, __: six.StringIO(data[p]) loader = FluentResourceLoader('{locale}') resources_list = list(loader.resources('en', ['one.ftl', 'two.ftl'])) self.assertEqual(len(resources_list), 0)
def test_one_exists(self, codecs_open, isfile): data = { 'en/two.ftl': 'two = exists', } isfile.side_effect = lambda p: p in data codecs_open.side_effect = lambda p, _, __: io.StringIO(data[p]) loader = FluentResourceLoader('{locale}') resources_list = list(loader.resources('en', ['one.ftl', 'two.ftl'])) self.assertEqual(len(resources_list), 1) resources = resources_list[0] self.assertEqual(len(resources), 1)
def test_bundles(self, codecs_open, isfile): data = { 'de/one.ftl': 'one = in German', 'de/two.ftl': 'two = in German', 'fr/two.ftl': 'three = in French', 'en/one.ftl': 'four = exists', 'en/two.ftl': 'five = exists', } isfile.side_effect = lambda p: p in data or ISFILE(p) codecs_open.side_effect = lambda p, _, __: six.StringIO(data[p]) l10n = FluentLocalization(['de', 'fr', 'en'], ['one.ftl', 'two.ftl'], FluentResourceLoader('{locale}')) bundles_gen = l10n._bundles() bundle_de = next(bundles_gen) self.assertEqual(bundle_de.locales[0], 'de') self.assertTrue(bundle_de.has_message('one')) self.assertTrue(bundle_de.has_message('two')) bundle_fr = next(bundles_gen) self.assertEqual(bundle_fr.locales[0], 'fr') self.assertFalse(bundle_fr.has_message('one')) self.assertTrue(bundle_fr.has_message('three')) self.assertListEqual(list(l10n._bundles())[:2], [bundle_de, bundle_fr]) bundle_en = next(bundles_gen) self.assertEqual(bundle_en.locales[0], 'en') self.assertEqual(l10n.format_value('one'), 'in German') self.assertEqual(l10n.format_value('two'), 'in German') self.assertEqual(l10n.format_value('three'), 'in French') self.assertEqual(l10n.format_value('four'), 'exists') self.assertEqual(l10n.format_value('five'), 'exists')
def fluent_l10n(locales, files): if isinstance(locales, str): locales = [locales] # file IDs may not have file extension files = [f'{f}.ftl' if not f.endswith('.ftl') else f for f in files] paths = [f'{path}/{{locale}}/' for path in settings.FLUENT_PATHS] loader = FluentResourceLoader(paths) return FluentL10n(locales, files, loader)
def get_injector(resources: List[str]) -> Any: try: locale = get_config_value("Tsundoku", "locale") except KeyError: locale = "en" loader = FluentResourceLoader("l10n/{locale}") resources = [f"{r}.ftl" for r in resources] fluent = FluentLocalization([locale, "en"], resources, loader) fluent._ = fluent.format_value return fluent
def __init__(self, asset_path, *args, **kwargs): super().__init__(*args, **kwargs) self.asset_path = Path(asset_path) self.assets = { Server.JP: AssetManager(self.asset_path, timezone=pytz.timezone('Asia/Tokyo'), drop_extra_fields=True), Server.EN: AssetManager(self.asset_path.with_name(self.asset_path.name + '_en'), timezone=pytz.timezone('UTC'), drop_extra_fields=True), } self.fluent_loader = FluentResourceLoader('l10n/{locale}') self.aliases = CommonAliases(self.assets) self.master_filters = MasterFilterManager(self) self.session = aiohttp.ClientSession() self.extension_names = set() self.thread_pool = ThreadPoolExecutor() self.scripts_path = None
def test_init(self): l10n = FluentLocalization(['en'], ['file.ftl'], FluentResourceLoader('{locale}')) self.assertTrue(callable(l10n.format_value))
# License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. from pathlib import Path from unittest.mock import patch from django.conf import settings from django.test import TestCase, override_settings from fluent.runtime import FluentResourceLoader from lib.l10n_utils import fluent from lib.l10n_utils import translation L10N_PATH = Path(__file__).with_name('test_files').joinpath('l10n') TEST_LOADER = FluentResourceLoader(f'{L10N_PATH}/{{locale}}/') def get_l10n(locales=None, ftl_files=None): locales = locales or ['de', 'en'] ftl_files = ftl_files or ['mozorg/fluent.ftl'] return fluent.FluentL10n(locales, ftl_files, TEST_LOADER) class TestFluentL10n(TestCase): def test_localized_bundles(self): l10n = get_l10n() bundles = list(l10n._bundles()) localized_bundles = list(l10n._localized_bundles()) assert len(bundles) == 2 assert len(localized_bundles) == 1
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. import os import re from django.conf import settings from fluent.runtime import FluentResourceLoader FTL_LOADER = FluentResourceLoader(f"{settings.FLUENT_LOCAL_PATH}/{{locale}}/") COMMENT_RE = re.compile(r"LANG_ID_HASH: (\w{32})") class ContainsEverything: """An object whose instances will claim to contain anything.""" def __contains__(self, item): return True def strip_whitespace(message): """Collapses all whitespace into single spaces. Borrowed from Tower. """ return re.compile(r"\s+", re.UNICODE).sub(" ", message).strip() def get_l10n_path(path): """Generate the path to a lang file from a django path. /apps/foo/templates/foo/bar.html -> foo/bar
from fluent.runtime import FluentLocalization, FluentResourceLoader loader = FluentResourceLoader("l10n/{locale}") l10n = FluentLocalization(["en-US"], ["main.ftl"], loader)