Пример #1
0
    def process_resource(self,
                         source: str,
                         output_file: str,
                         include_dependencies: list,
                         exclude_dependencies: list,
                         options: dict = {}):
        if not options: raise ValueError('I18nProcessor [options] must be set')
        if 'i18n_dependencies' not in options or not options[
                'i18n_dependencies']:
            raise ValueError(
                'I18nProcessor expect options to contain \'i18n_dependencies\' list to filter'
            )
        if 'language' not in options or not options['language']:
            raise ValueError(
                'I18nProcessor expect options to contain \'language\' ')

        translation = DjangoTranslation(options['language'])
        new_pattern = '|'.join([
            '({})'.format(pattern) for pattern in options['i18n_dependencies']
            if pattern
        ])
        result = {}
        for key, value in translation._catalog.items():
            if key and isinstance(key, str) and re.match(new_pattern, key):
                result[key] = value

        with open(output_file, 'w+') as fp:
            json.dump(result, fp)
            fp.close()
Пример #2
0
 def render_to_str(self):
     # hardcoding locales as it is not trivial to
     # get user apps and its locales, and including
     # all django supported locales is not efficient
     codes = ['en', 'de', 'ru', 'es', 'fr', 'pt']
     catalog = {}
     plural = {}
     # this function is not i18n-enabled
     formats = get_formats()
     for code in codes:
         self.translation = DjangoTranslation(code,
                                              domain=self.domain)
         _catalog = self.get_catalog()
         _plural = self.get_plural()
         if _catalog:
             catalog[code] = _catalog
         if _plural:
             if six.PY2:
                 _plural = _plural.__str__()
             plural[code] = _plural
     template = Engine().from_string(JS_CATALOG_TEMPLATE)
     context = {
         'catalog_str': json.dumps(catalog,
                                   sort_keys=True,
                                   indent=2),
         'formats_str': json.dumps(formats,
                                   sort_keys=True,
                                   indent=2),
         'plural': plural,
     }
     return template.render(Context(context))
Пример #3
0
def generate_widget_js(lang):
    code = []
    with language(lang):
        # Provide isolation
        code.append('(function (siteglobals) {\n')
        code.append('var module = {}, exports = {};\n')
        code.append('var lang = "%s";\n' % lang)

        c = JavaScriptCatalog()
        c.translation = DjangoTranslation(lang, domain='djangojs')
        catalog, plural = c.get_catalog(), c.get_plural()

        str_wl = ('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su', 'January',
                  'February', 'March', 'April', 'May', 'June', 'July',
                  'August', 'September', 'October', 'November', 'December')
        catalog = dict((k, v) for k, v in catalog.items()
                       if k.startswith('widget\u0004') or k in str_wl)
        template = Engine().from_string(js_catalog_template)
        context = Context({
            'catalog_str':
            indent(json.dumps(catalog, sort_keys=True, indent=2))
            if catalog else None,
            'formats_str':
            indent(json.dumps(get_formats(), sort_keys=True, indent=2)),
            'plural':
            plural,
        })
        i18n_js = template.render(context)
        i18n_js = i18n_js.replace(
            'for (const ',
            'for (var ')  # remove if we really want to break IE11 for good
        i18n_js = i18n_js.replace(
            r"value.includes(", r"-1 != value.indexOf("
        )  # remove if we really want to break IE11 for good
        code.append(i18n_js)

        files = [
            'vuejs/vue.js' if settings.DEBUG else 'vuejs/vue.min.js',
            'vuejs/vue-resize.min.js',
            'pretixpresale/js/widget/docready.js',
            'pretixpresale/js/widget/floatformat.js',
            'pretixpresale/js/widget/widget.js',
        ]
        for fname in files:
            f = finders.find(fname)
            with open(f, 'r', encoding='utf-8') as fp:
                code.append(fp.read())

        if settings.DEBUG:
            code.append('})(this);\n')
        else:
            # Do not expose debugging variables
            code.append('})({});\n')
    code = ''.join(code)
    code = rJSMinFilter(content=code).output()
    return code
Пример #4
0
 def get(self, request, *args, **kwargs):
     locale = get_language()
     domain = kwargs.get('domain', self.domain)
     # If packages are not provided, default to all installed packages, as
     # DjangoTranslation without localedirs harvests them all.
     packages = kwargs.get('packages', '').split('+') or self.packages
     paths = self.get_paths(packages) if packages else None
     self.translation = DjangoTranslation(locale, domain=domain, localedirs=paths)
     context = self.get_context_data(**kwargs)
     return self.render_to_response(context)
 def __init__(self, *args, **kw):
     # Django 1.7:
     #    def __init__(self, *args, **kw):
     #        gettext_module.GNUTranslations.__init__(self, *args, **kw)
     # Django 1.8:
     #    def __init__(self, language):
     #        gettext_module.GNUTranslations.__init__(self)
     #        self.__language = language
     # XXX Django 1.8 prototype is very strange since the call
     #     from gettext.py will be:
     #         with open(mofile, 'rb') as fp:
     #             t = _translations.setdefault(key, class_(fp))
     if DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] < 8:
         DjangoTranslation.__init__(self, *args, **kw)
     else:
         DjangoTranslation.__init__(self, 'en-us')
     self._catalog = {}
     self.set_output_charset('utf-8')
     self.__language = 'en-us'
Пример #6
0
 def get(self, request, *args, **kwargs):
     locale = get_language()
     domain = kwargs.get('domain', self.domain)
     packages = kwargs.get('packages', '')
     packages = packages.split('+') if packages else self.packages
     paths = self.get_paths(packages) if packages else None
     if isinstance(domain, list) or isinstance(domain, tuple):
         if len(domain) > 0:
             for i, domainn in enumerate(domain):
                 if i == 0:
                     self.translation = DjangoTranslation(locale, domain=domainn, localedirs=paths)
                 else:
                     self.translation.merge(DjangoTranslation(locale, domain=domainn, localedirs=paths))
         else:
             domain = 'djangojs'
     if isinstance(domain, str):
         self.translation = DjangoTranslation(locale, domain=domain, localedirs=paths)
     context = self.get_context_data(**kwargs)
     return self.render_to_response(context)
Пример #7
0
def get_translation_object(*, django_language_code: str,
                           domain: str) -> DjangoTranslation:
    """
    Return a DjangoTranslation object suitable to activate
    when we're wanting to render templates for this language code and domain.
    (The domain is typically specific to one or a few licenses that
    have common translations.)
    """

    license_locale_dir = os.path.join(
        settings.TRANSLATION_REPOSITORY_DIRECTORY, "translations")
    # Start with a translation object for the domain for this license.
    license_translation_object = DjangoTranslation(
        language=django_language_code,
        domain=domain,
        localedirs=[license_locale_dir])
    # Add a fallback to the standard Django translation for this language. This gets us the
    # non-legalcode parts of the pages.
    license_translation_object.add_fallback(translation(django_language_code))

    return license_translation_object
Пример #8
0
    def get_catalog(self, locale):
        """Create Django translation catalogue for `locale`."""
        with translation.override(locale):
            try:
                translation_engine = DjangoTranslation(locale, domain=self.domain, localedirs=self.paths)

                trans_cat = translation_engine._catalog
                trans_fallback_cat = translation_engine._fallback._catalog if translation_engine._fallback else {}
            except:
                trans_cat = get_catalogue_legacy(self.domain, locale, self.packages)
                trans_fallback_cat = get_catalogue_legacy(self.domain, settings.LANGUAGE_CODE, self.packages)

            return trans_cat, trans_fallback_cat
Пример #9
0
def get_javascript_catalog(locale, domain, packages):
    app_configs = apps.get_app_configs()
    allowable_packages = set(app_config.name for app_config in app_configs)
    allowable_packages.update(DEFAULT_PACKAGES)
    packages = [p for p in packages if p in allowable_packages]
    paths = []
    # paths of requested packages
    for package in packages:
        p = importlib.import_module(package)
        path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
        paths.append(path)

    trans = DjangoTranslation(locale, domain=domain, localedirs=paths)
    trans_cat = trans._catalog

    plural = None
    if '' in trans_cat:
        for line in trans_cat[''].split('\n'):
            if line.startswith('Plural-Forms:'):
                plural = line.split(':', 1)[1].strip()
    if plural is not None:
        # this should actually be a compiled function of a typical plural-form:
        # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
        #               n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
        plural = [
            el.strip() for el in plural.split(';')
            if el.strip().startswith('plural=')
        ][0].split('=', 1)[1]

    pdict = {}
    maxcnts = {}
    catalog = {}
    trans_fallback_cat = trans._fallback._catalog if trans._fallback else {}
    for key, value in itertools.chain(six.iteritems(trans_cat),
                                      six.iteritems(trans_fallback_cat)):
        if key == '' or key in catalog:
            continue
        if isinstance(key, six.string_types):
            catalog[key] = value
        elif isinstance(key, tuple):
            msgid = key[0]
            cnt = key[1]
            maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
            pdict.setdefault(msgid, {})[cnt] = value
        else:
            raise TypeError(key)
    for k, v in pdict.items():
        catalog[k] = [v.get(i, '') for i in range(maxcnts[k] + 1)]

    return catalog, plural
Пример #10
0
 def test_license_deed_view_code_version_jurisdiction_language(self):
     license = LicenseFactory(
         license_code="by-nc", jurisdiction_code="es", version="3.0"
     )
     language_code = "fr"
     lc = LegalCodeFactory(license=license, language_code=language_code)
     # "<code:license_code>/<version:version>/<jurisdiction:jurisdiction>/deed.<lang:target_lang>"
     url = lc.deed_url
     # Mock 'get_translation_object' because we have no 3.0 translations imported yet
     # and we can't use 4.0 to test jurisdictions.
     translation_object = DjangoTranslation(language="fr")
     with mock.patch.object(LegalCode, "get_translation_object") as mock_gto:
         mock_gto.return_value = translation_object
         rsp = self.client.get(url)
     self.assertEqual(200, rsp.status_code)
Пример #11
0
def generate_widget_js(lang):
    code = []
    with language(lang):
        # Provide isolation
        code.append('(function (siteglobals) {\n')
        code.append('var module = {}, exports = {};\n')
        code.append('var lang = "%s";\n' % lang)

        c = JavaScriptCatalog()
        c.translation = DjangoTranslation(lang, domain='djangojs')
        catalog, plural = c.get_catalog(), c.get_plural()

        catalog = dict(
            (k, v) for k, v in catalog.items() if k.startswith('widget\u0004'))
        template = Engine().from_string(js_catalog_template)
        context = Context({
            'catalog_str':
            indent(json.dumps(catalog, sort_keys=True, indent=2))
            if catalog else None,
            'formats_str':
            indent(json.dumps(get_formats(), sort_keys=True, indent=2)),
            'plural':
            plural,
        })
        code.append(template.render(context))

        files = [
            'vuejs/vue.js' if settings.DEBUG else 'vuejs/vue.min.js',
            'pretixpresale/js/widget/docready.js',
            'pretixpresale/js/widget/floatformat.js',
            'pretixpresale/js/widget/widget.js',
        ]
        for fname in files:
            f = finders.find(fname)
            with open(f, 'r') as fp:
                code.append(fp.read())

        if settings.DEBUG:
            code.append('})(this);\n')
        else:
            # Do not expose debugging variables
            code.append('})({});\n')
    return ''.join(code)
Пример #12
0
    def test_view_deed_jurisdiction_language(self):
        lc = LegalCode.objects.filter(
            tool__unit="by-sa",
            tool__version="3.0",
            tool__jurisdiction_code="es",
        )[0]
        tool = lc.tool

        language_code = "fr"
        lc = LegalCodeFactory(tool=tool, language_code=language_code)
        # "<code:unit>/<version:version>/<jurisdiction:jurisdiction>
        #  /deed.<lang:target_lang>"
        url = lc.deed_url
        # Mock 'get_translation_object' because we have no 3.0 translations
        # imported yet and we can't use 4.0 to test jurisdictions.
        translation_object = DjangoTranslation(language="fr")
        with mock.patch.object(LegalCode,
                               "get_translation_object") as mock_gto:
            mock_gto.return_value = translation_object
            rsp = self.client.get(url)
        self.assertEqual(200, rsp.status_code)
Пример #13
0
    def test_js_render(self):
        prop_data_string_en = '--i-am-props-data-string---'
        i18n_string_en = "Where you want to be in your next relationship"

        hebrew_translation = DjangoTranslation('he')
        prop_data_string_he = hebrew_translation._catalog[
            'CONST_USER_open_question_placeholder_1']
        i18n_string_he = hebrew_translation._catalog[
            'CONST_USER_open_question_placeholder_4']

        response_en = BigpipeResponse(
            HttpResponse(),
            render_type=BigpipeResponse.RenderType.JAVASCRIPT_RENDER,
            render_source='TestMainPage',
            render_context={'props_data': prop_data_string_en},
            i18n_dependencies=[
                "CONST_USER_open_question_placeholder.*",
                "profileboxes_no_more_profiles"
            ])
        response_str_en = list(response_en)[0].decode("utf-8")
        self.assertEqual(response_str_en[0:5], '<html')
        self.assertGreater(response_str_en.index(prop_data_string_en), 10)
        self.assertGreater(response_str_en.index(i18n_string_en), 10)

        activate('he')

        response_he = BigpipeResponse(
            HttpResponse(),
            render_type=BigpipeResponse.RenderType.JAVASCRIPT_RENDER,
            render_source='TestMainPage',
            render_context={'props_data': prop_data_string_he},
            i18n_dependencies=[
                "CONST_USER_open_question_placeholder.*",
                "profileboxes_no_more_profiles"
            ])
        response_str_he = list(response_he)[0].decode("utf-8")
        self.assertEqual(response_str_he[0:5], '<html')
        self.assertGreater(response_str_he.index(prop_data_string_he), 10)
        self.assertGreater(response_str_he.index(i18n_string_he), 10)
Пример #14
0
def js_catalog(request, lang):
    c = JavaScriptCatalog()
    c.translation = DjangoTranslation(lang, domain='djangojs')
    return render_javascript_catalog(c.get_catalog(), c.get_plural())
Пример #15
0
 def _init_translation_catalog(self, *args, **kwargs):
     self._catalog = {}
     DjangoTranslation._init_translation_catalog(self, *args, **kwargs)
Пример #16
0
 def __init__(self, language, tenant_name):
     self.tenant_name = tenant_name
     DjangoTranslationOriginal.__init__(self, language)
Пример #17
0
 def translation(language):
     global _translations
     if language not in _translations:
         _translations[language] = DjangoTranslation(language,
                                                     domain='dmoj-user')
     return _translations[language]
Пример #18
0
def js_catalog(request, lang):
    c = JavaScriptCatalog()
    c.translation = DjangoTranslation(lang, domain='djangojs')
    context = c.get_context_data()
    return c.render_to_response(context)
Пример #19
0
 def _init_translation_catalog(self, *args, **kwargs):
     self._catalog = {}
     DjangoTranslation._init_translation_catalog(self, *args, **kwargs)