Example #1
0
File: core.py Project: DaC24/taskr
 def test_locale_comparison(self):
     en_US = Locale('en', 'US')
     self.assertEqual(en_US, en_US)
     self.assertNotEqual(None, en_US)
     
     bad_en_US = Locale('en_US')
     self.assertNotEqual(en_US, bad_en_US)
Example #2
0
    def settarget(self, target):
        if (self.hasplurals(self.source) or self.hasplurals(target)):
            # Fix the root tag if mismatching
            if self.xmlelement.tag != "plurals":
                old_id = self.getid()
                self.xmlelement = etree.Element("plurals")
                self.xmlelement.tail = '\n'
                self.setid(old_id)

            try:
                lang_tags = set(Locale(self.gettargetlanguage()).plural_form.tags)
            except UnknownLocaleError:
                lang_tags = set(Locale('en').plural_form.tags)
            # Ensure that the implicit default "other" rule is present (usually omitted by Babel)
            lang_tags.add('other')

            # Get plural tags in the right order.
            plural_tags = [tag for tag in ['zero', 'one', 'two', 'few', 'many', 'other'] if tag in lang_tags]

            # Get string list to handle, wrapping non multistring/list targets into a list.
            if isinstance(target, multistring):
                plural_strings = target.strings
            elif isinstance(target, list):
                plural_strings = target
            else:
                plural_strings = [target]

            # Sync plural_strings elements to plural_tags count.
            if len(plural_strings) < len(plural_tags):
                plural_strings += [''] * (len(plural_tags) - len(plural_strings))
            plural_strings = plural_strings[:len(plural_tags)]

            # Rebuild plurals.
            for entry in self.xmlelement.iterchildren():
                self.xmlelement.remove(entry)

            self.xmlelement.text = "\n\t"

            for plural_tag, plural_string in zip(plural_tags, plural_strings):
                item = etree.Element("item")
                item.set("quantity", plural_tag)
                self.set_xml_text_value(plural_string, item)
                item.tail = "\n\t"
                self.xmlelement.append(item)
            # Remove the tab from last item
            item.tail = "\n"
        else:
            # Fix the root tag if mismatching
            if self.xmlelement.tag != "string":
                old_id = self.getid()
                self.xmlelement = etree.Element("string")
                self.xmlelement.tail = '\n'
                self.setid(old_id)

            self.set_xml_text_value(target, self.xmlelement)

        super(AndroidResourceUnit, self).settarget(target)
Example #3
0
def test_locale_comparison():
    en_US = Locale('en', 'US')
    en_US_2 = Locale('en', 'US')
    fi_FI = Locale('fi', 'FI')
    bad_en_US = Locale('en_US')
    assert en_US == en_US
    assert en_US == en_US_2
    assert en_US != fi_FI
    assert not (en_US != en_US_2)
    assert en_US is not None
    assert en_US != bad_en_US
    assert fi_FI != bad_en_US
Example #4
0
    def test_locale_gets_overridden_by_file(self):
        buf = StringIO(r'''
msgid ""
msgstr ""
"Language: en_US\n"''')
        catalog = pofile.read_po(buf, locale='de')
        self.assertEqual(Locale('en', 'US'), catalog.locale)
        buf = StringIO(r'''
msgid ""
msgstr ""
"Language: ko-KR\n"''')
        catalog = pofile.read_po(buf, locale='de')
        self.assertEqual(Locale('ko', 'KR'), catalog.locale)
Example #5
0
def get_accepted_languages() -> List[str]:
    """
    Convert a request's list of accepted languages into locale identifiers.
    """
    accept_languages = []
    for l in request.accept_languages.values():
        try:
            parsed = Locale.parse(l, "-")
            accept_languages.append(str(parsed))

            # We only have two Chinese translations, simplified
            # and traditional, based on script and not
            # region. Browsers tend to send identifiers with
            # region, e.g. zh-CN or zh-TW. Babel can generally
            # infer the script from those, so we can fabricate a
            # fallback entry without region, in the hope that it
            # will match one of our translations and the site will
            # at least be more legible at first contact than the
            # probable default locale of English.
            if parsed.language == "zh" and parsed.script:
                accept_languages.append(
                    str(Locale(language=parsed.language,
                               script=parsed.script)))
        except (ValueError, UnknownLocaleError):
            pass
    return accept_languages
Example #6
0
    def _parse_page_basename(self, basename):
        """Split the page basename into the article id and locale.

        `basename` is (supposed to be) of the form
        `<article_id>.<locale>`, e.g. `news.en`.

        If either there is no dot or the locale is unknown,
        the `default_locale` of babel is used.

        :return: The tuple `(article_id, locale)`.
        """
        default_locale = self.extension.app.babel_instance.default_locale
        article_id, sep, locale_identifier = basename.rpartition('.')

        if sep == '':
            return basename, default_locale

        try:
            locale = Locale(locale_identifier)
        except UnknownLocaleError:
            logger.error("Unknown locale %s of arcticle %s",
                         locale_identifier, basename)
            return basename, default_locale
        if locale not in possible_locales():
            logger.warning("Locale %s of article is not a possible locale",
                           locale_identifier, basename)
            return basename, default_locale
        return article_id, locale
Example #7
0
 def test_format_number(self, format_number):
     ctx = {'LANG': 'de'}
     locale = Locale('de')
     eq_(helpers.l10n_format_number(ctx, 10000),
         format_number.return_value)
     format_number.assert_called_with(
         10000, locale=locale)
Example #8
0
 def test_format_number_hyphenated_locale(self, format_number):
     ctx = {'LANG': 'pt-BR'}
     locale = Locale('pt', 'BR')
     eq_(helpers.l10n_format_number(ctx, 10000),
         format_number.return_value)
     format_number.assert_called_with(
         10000, locale=locale)
Example #9
0
def get_metadata(app, docname):
    '''
    Extracts metadata from a document.
    '''
    env = app.builder.env
    language = app.config.language
    locale = Locale.parse(language) if language else Locale('en', 'US')
    format_ui_date = partial(
        format_date, format=UIStr.TIMESTAMP_FMT, locale=locale)
    format_short_ui_short = partial(
        format_date, format=UIStr.TIMESTAMP_FMT_SHORT, locale=locale)

    env.blog_metadata[docname] = Metadata()
    metadata = env.blog_metadata[docname]

    # if it's a page
    if docname.startswith("pages/"):
      metadata.is_page = True
      return

    # posts are identified by ($YEAR)/($MONTH)/($DAY) paths
    match = re.match(r"\d{4}/\d{2}/\d{2}/", docname)

    # if not post return
    if not match:
        return

    metadata.is_post = True
    metadata.link = docname
    metadata.date = datetime.datetime.strptime(match.group(), "%Y/%m/%d/")

    # we format date here instead of inside template due to localization issues
    # and Python2 vs Python3 incompatibility
    metadata.formatted_date = format_ui_date(metadata.date)
    metadata.formatted_date_short = format_short_ui_short(metadata.date)
Example #10
0
def extract_and_update_locale_dir(dirname, mapping):
    locale_dir = os.path.join(dirname, 'locale')

    for domain in ('django', 'djangojs'):
        extracted_catalog = Catalog()
        extracted = extract_from_dir(
            dirname=dirname,
            method_map=mapping[domain]['method_map'],
            options_map=mapping[domain]['options_map']
        )
        for filename, lineno, message, comments, context in extracted:
            extracted_catalog.add(message, None, [(filename, lineno)], auto_comments=comments, context=context)

        for locale, language in settings.LANGUAGES:
            po_path = os.path.join(locale_dir, locale, 'LC_MESSAGES', '%s.po' % domain)

            if os.path.exists(po_path):
                with open(po_path, 'rb') as po_fileobj:
                    catalog = read_po(po_fileobj, locale=locale, domain=domain)
                if catalog._messages != extracted_catalog._messages:
                    catalog.update(extracted_catalog, no_fuzzy_matching=True, update_header_comment=False)
                    with open(po_path, 'wb') as po_fileobj:
                        write_po(po_fileobj, catalog, ignore_obsolete=True)

            elif extracted_catalog._messages:
                extracted_catalog.locale = Locale(locale)
                extracted_catalog.domain = domain
                extracted_catalog.project = 'Mesto.UA'
                extracted_catalog.copyright_holder = 'Mesto.UA'
                extracted_catalog.fuzzy = False
                touch_directory(po_path)
                with open(po_path, 'wb') as po_fileobj:
                    write_po(po_fileobj, extracted_catalog)
Example #11
0
 def test_unknown(self):
     """
     Test that when the current locale is not supported by Babel, it
     defaults to en-US.
     """
     activate('fy')
     eq_(Locale('en', 'US'), current_locale())
Example #12
0
def get_locale(lang):
    """Return a babel Locale object for lang. defaults to LANGUAGE_CODE."""
    lang = babel_format_locale_map.get(lang) or lang
    try:
        return Locale.parse(lang, sep="-")
    except (UnknownLocaleError, ValueError):
        return Locale(*settings.LANGUAGE_CODE.split("-"))
    def parse(self) -> PriceRequest:
        text = self.text

        obj = AMOUNT_PATTERN_COMPILED.match(text)
        if not obj:
            raise WrongFormatException

        amount = obj[0]

        if amount:
            amount = parse_amount(amount, self.locale)

        last_request = (Session.query(ChatRequests).filter_by(
            chat_id=self.chat_id).order_by(
                ChatRequests.modified_at.desc()).first())

        if not last_request:
            raise WrongFormatException

        locale = Locale(self.locale)

        if locale.character_order == "right-to-left":
            direction_writing = DirectionWriting.RIGHT2LEFT
        else:
            direction_writing = DirectionWriting.LEFT2RIGHT

        return PriceRequest(
            amount=amount,
            currency=last_request.from_currency.code,
            to_currency=last_request.to_currency.code,
            parser_name=self.name,
            direction_writing=direction_writing,
        )
Example #14
0
def _babel_locale():
    """Return the current locale in Babel's format."""
    try:
        return Locale.parse(get_language(), sep='-')
    except UnknownLocaleError:
        # Default to en-US
        return Locale('en', 'US')
Example #15
0
 def test_format_date_hyphenated_locale(self, format_date):
     ctx = {'LANG': 'en-US'}
     locale = Locale('en', 'US')
     eq_(helpers.l10n_format_date(ctx, 'somedate', format='long'),
         format_date.return_value)
     format_date.assert_called_with(
         'somedate', locale=locale, format='long')
Example #16
0
def load_i18n(project_root, tell_sentry):
    # Load the locales
    localeDir = os.path.join(project_root, 'i18n', 'core')
    locales = LOCALES
    for file in os.listdir(localeDir):
        try:
            parts = file.split(".")
            if not (len(parts) == 2 and parts[1] == "po"):
                continue
            lang = parts[0]
            with open(os.path.join(localeDir, file)) as f:
                l = locales[lang.lower()] = Locale(lang)
                c = l.catalog = read_po(f)
                c.plural_func = get_function_from_rule(c.plural_expr)
                try:
                    l.countries = make_sorted_dict(COUNTRIES, l.territories)
                except KeyError:
                    l.countries = COUNTRIES
                try:
                    l.languages_2 = make_sorted_dict(LANGUAGES_2, l.languages)
                except KeyError:
                    l.languages_2 = LANGUAGES_2
        except Exception as e:
            tell_sentry(e, {})

    # Add aliases
    for k, v in list(locales.items()):
        locales.setdefault(ALIASES.get(k, k), v)
        locales.setdefault(ALIASES_R.get(k, k), v)
    for k, v in list(locales.items()):
        locales.setdefault(k.split('_', 1)[0], v)

    # Patch the locales to look less formal
    locales['fr'].currency_formats[None] = parse_pattern('#,##0.00\u202f\xa4')
    locales['fr'].currency_symbols['USD'] = '$'
Example #17
0
def main(src, dst):
    locales = []
    for f in path.path(src).walkfiles('messages.po'):
        print f
        lang = f.split('/')[1]
        locales.append(lang.replace('_', '-'))
        print lang
        try:
            locale = Locale(lang)
        except UnknownLocaleError:
            print 'Unknown locale:', lang
            locale = Locale(DEFAULT)
        out = path.path(dst) / lang
        if not out.exists():
            out.makedirs()
        d = {
            'po': json.dumps(po_to_dict(f), separators=(',', ':')),
            'timefmt': locale.time_formats['short'].pattern,
            'numfmt': locale.decimal_formats[None].pattern,
            'group': locale.number_symbols['group']
        }
        print '% 5s %8s %s %s' % (lang, d['timefmt'], d['group'], d['numfmt'])
        with codecs.open(out / 'l10n.js', 'w', 'utf-8') as fd:
            fd.write(template % d)

        default = path.path('locale/countries/en-US.json')
        countries = path.path('locale/countries/%s.json' %
                              lang.replace('_', '-'))
        regions = path.path('locale/%s/regions.json' % lang)
        cities = path.path('locale/%s/cities.json' % lang)
        if not countries.exists():
            print '*' * 30, 'missing', lang
            countries = default
        with codecs.open(out / 'countries.js', 'w', 'utf-8') as fd:
            d = dict(
                (k.upper(), v) for k, v in json.load(countries.open()).items())
            if regions.exists():
                print 'Adding regions for', lang
                d.update(json.load(regions.open()))
            if cities.exists():
                print 'Adding cities for', lang
                d.update(json.load(cities.open()))
            fd.write('var _countries = %s;' %
                     json.dumps(d, separators=(',', ':')))
    lo = ["'%s'" % x.lower() for x in locales]
    print '$locales = array(%s);' % ', '.join(lo)
Example #18
0
def _get_babel_locale(red_locale: str) -> babel.core.Locale:
    supported_locales = babel.localedata.locale_identifiers()
    try:  # Handles cases where red_locale is already Babel supported
        babel_locale = Locale(*babel.parse_locale(red_locale))
    except (ValueError, babel.core.UnknownLocaleError):
        try:
            babel_locale = Locale(*babel.parse_locale(red_locale, sep="-"))
        except (ValueError, babel.core.UnknownLocaleError):
            # ValueError is Raised by `parse_locale` when an invalid Locale is given to it
            # Lets handle it silently and default to "en_US"
            try:
                # Try to find a babel locale that's close to the one used by red
                babel_locale = Locale(Locale.negotiate([red_locale], supported_locales, sep="-"))
            except (ValueError, TypeError, babel.core.UnknownLocaleError):
                # If we fail to get a close match we will then default to "en_US"
                babel_locale = Locale("en", "US")
    return babel_locale
Example #19
0
 def test_format_date_hyphenated_locale(self, format_date):
     ctx = {"LANG": "en-US"}
     locale = Locale("en", "US")
     assert helpers.l10n_format_date(
         ctx, "somedate", format="long") == format_date.return_value
     format_date.assert_called_with("somedate",
                                    locale=locale,
                                    format="long")
Example #20
0
 def test_format_date(self, format_date):
     ctx = {'LANG': 'de'}
     locale = Locale('de')
     assert (helpers.l10n_format_date(
         ctx, 'somedate', format='long') == format_date.return_value)
     format_date.assert_called_with('somedate',
                                    locale=locale,
                                    format='long')
Example #21
0
def format_date(dt, locale_name=None, format="medium"):
    """Returns a prettyfied version of a date. If a locale_name is
    provided the date will be localized. Without a locale the
    datetime will be formatted into the form YYYY-MM-DD hh:ss"""
    if locale_name:
        locale = Locale(locale_name)
        return babel_format_date(dt, locale=locale, format=format)
    return dt.strftime("%Y-%m-%d")
Example #22
0
 def __init__(self, request):
     self.request = request
     self.context = request.context
     self.root = request.root
     self.locale = Locale(request.locale_name)
     self.display_days = int(request.registry.settings['display_days'])
     self.delta_period = (datetime.datetime.now() -
                          datetime.timedelta(days=self.display_days))
     self.fd = format_datetime
Example #23
0
def current_locale():
    """
    Return the current Locale object (from Babel). Defaults to locale
    based on settings.LANGUAGE_CODE if locale does not exist.
    """
    try:
        return Locale.parse(get_language(), sep='-')
    except (UnknownLocaleError, ValueError):
        return Locale(*settings.LANGUAGE_CODE.split('-'))
Example #24
0
def load_i18n(project_root, tell_sentry):
    # Load the locales
    key = lambda t: strip_accents(t[1])
    localeDir = os.path.join(project_root, 'i18n', 'core')
    locales = i18n.LOCALES
    for file in os.listdir(localeDir):
        try:
            parts = file.split(".")
            if not (len(parts) == 2 and parts[1] == "po"):
                continue
            lang = parts[0]
            with open(os.path.join(localeDir, file)) as f:
                l = locales[lang.lower()] = Locale(lang)
                c = l.catalog = read_po(f)
                c.plural_func = get_function_from_rule(c.plural_expr)
                try:
                    l.countries_map = {
                        k: l.territories[k]
                        for k in COUNTRIES_MAP
                    }
                    l.countries = sorted(l.countries_map.items(), key=key)
                except KeyError:
                    l.countries_map = COUNTRIES_MAP
                    l.countries = COUNTRIES
        except Exception as e:
            tell_sentry(e)

    # Add the default English locale
    locale_en = i18n.LOCALE_EN = locales['en'] = Locale('en')
    locale_en.catalog = Catalog('en')
    locale_en.catalog.plural_func = lambda n: n != 1
    locale_en.countries = COUNTRIES
    locale_en.countries_map = COUNTRIES_MAP

    # Add aliases
    for k, v in list(locales.items()):
        locales.setdefault(ALIASES.get(k, k), v)
        locales.setdefault(ALIASES_R.get(k, k), v)
    for k, v in list(locales.items()):
        locales.setdefault(k.split('_', 1)[0], v)

    # Patch the locales to look less formal
    locales['fr'].currency_formats[None] = parse_pattern('#,##0.00\u202f\xa4')
    locales['fr'].currency_symbols['USD'] = '$'
Example #25
0
def current_locale():
    """
    Return the current Locale object (from Babel). Defaults to en-US if locale
    does not exist.
    """
    try:
        return Locale.parse(get_language(), sep='-')
    except UnknownLocaleError:
        # Default to en-US
        return Locale('en', 'US')
Example #26
0
def get_human_size(value, request=None):
    """Convert given bytes value in human readable format
    """
    if request is None:
        request = check_request()
    translate = request.localizer.translate
    try:
        locale = Locale(request.locale_name)
    except UnknownLocaleError:
        locale = Locale(
            request.registry.settings.get('pyramid.default_locale_name', 'en'))
    if value < 1024:
        return format_decimal(value, translate(_('0 bytes')), locale)
    value /= 1024
    if value < 1024:
        return format_decimal(value, translate(_('0.# Kb')), locale)
    value /= 1024
    if value < 1024:
        return format_decimal(value, translate(_('0.0# Mb')), locale)
    value /= 1024
    return format_decimal(value, translate(_('0.0## Gb')), locale)
Example #27
0
def get_languages():
    "return a list of supported languages"
    langs = (lang for lang in os.listdir(I18NPATH)
            if os.path.isdir(os.path.join(I18NPATH, lang)))

    def dictify(lang, name):
        "map function to make dict"
        retdict = {}
        retdict[lang] = name
        return retdict

    return (dictify(lang, Locale(lang).display_name)
            for lang in langs if check_language(lang))
Example #28
0
def render_table(
    locale: Locale,
    table: Table,
    source: Optional[str] = None,
) -> str:
    supported_locale_map: Mapping[str, AbstractSet[Locale]] = {
        locale.language:
        {l
         for l in table.supported_locales if l.language == locale.language}
        for locale in table.supported_locales
    }
    locales: Mapping[str, Union[Locale, Mapping[str, Locale]]] = {
        l: next(iter(ls)) if len(ls) == 1 else {
            '_': Locale(l),
            **{
                l.territory: l
                for l in sorted(ls,
                                key=lambda l: (
                                    l != locale,
                                    l.territory != locale.territory,
                                    get_territory_name(l.territory, locale),
                                ))
            }
        }
        for l, ls in sorted(supported_locale_map.items(),
                            key=lambda pair: (
                                pair[0] != 'en',
                                pair[0] != locale.language,
                                Locale(pair[0]).get_display_name(locale),
                            ))
    }
    return table_template.render(
        locale=locale,
        locales=locales,
        table=table,
        source=source,
    )
Example #29
0
 def _format_resource_items(self, items):
     """
     this wraps default implementation and for fields from custom schema
     it applies localized labels and values if possible
     """
     out = h.format_resource_items(items)
     new_out = []
     for key, val in items:
         if key == 'lang' and val:
             key = _("Language")
             loc = Locale(val)
             val = u'{} [{}]'.format(loc.display_name or loc.english_name,
                                     str(loc))
         new_out.append((key, val))
     return new_out
Example #30
0
def defaultLocale():
    try:
        lang, _ = locale.getdefaultlocale()
    except Exception:
        lang = None

    if lang is not None:
        try:
            return Locale.parse(lang)
        except UnknownLocaleError:
            pass
    else:
        try:
            return Locale.default()
        except UnknownLocaleError:
            return Locale('en', 'US')