def test_get_plural(): assert plurals.get_plural(locale='en') == (2, '(n != 1)') assert plurals.get_plural(locale='ga') == (3, '(n==1 ? 0 : n==2 ? 1 : 2)') tup = plurals.get_plural("ja") assert tup.num_plurals == 1 assert tup.plural_expr == '0' assert tup.plural_forms == 'npurals=1; plural=0' assert str(tup) == 'npurals=1; plural=0'
def num_plurals(self): if self._num_plurals is None: num = 2 if self.locale: num = get_plural(self.locale)[0] self._num_plurals = num return self._num_plurals
def plural_expr(self): if self._plural_expr is None: expr = "(n != 1)" if self.locale: expr = get_plural(self.locale)[1] self._plural_expr = expr return self._plural_expr
def test_get_plural(): # See http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html for more details. assert plurals.get_plural(locale='en') == (2, '(n != 1)') assert plurals.get_plural(locale='ga') == (3, '(n==1 ? 0 : n==2 ? 1 : 2)') plural_ja = plurals.get_plural("ja") assert str(plural_ja) == 'nplurals=1; plural=0;' assert plural_ja.num_plurals == 1 assert plural_ja.plural_expr == '0' assert plural_ja.plural_forms == 'nplurals=1; plural=0;' plural_en_US = plurals.get_plural('en_US') assert str(plural_en_US) == 'nplurals=2; plural=(n != 1);' assert plural_en_US.num_plurals == 2 assert plural_en_US.plural_expr == '(n != 1)' plural_fr_FR = plurals.get_plural('fr_FR') assert str(plural_fr_FR) == 'nplurals=2; plural=(n > 1);' assert plural_fr_FR.num_plurals == 2 assert plural_fr_FR.plural_expr == '(n > 1)' plural_pl_PL = plurals.get_plural('pl_PL') assert str( plural_pl_PL ) == 'nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);' assert plural_pl_PL.num_plurals == 3 assert plural_pl_PL.plural_expr == '(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'
def test_get_plural(): # See https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html for more details. assert plurals.get_plural(locale='en') == (2, '(n != 1)') assert plurals.get_plural(locale='ga') == (5, '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)') plural_ja = plurals.get_plural("ja") assert str(plural_ja) == 'nplurals=1; plural=0;' assert plural_ja.num_plurals == 1 assert plural_ja.plural_expr == '0' assert plural_ja.plural_forms == 'nplurals=1; plural=0;' plural_en_US = plurals.get_plural('en_US') assert str(plural_en_US) == 'nplurals=2; plural=(n != 1);' assert plural_en_US.num_plurals == 2 assert plural_en_US.plural_expr == '(n != 1)' plural_fr_FR = plurals.get_plural('fr_FR') assert str(plural_fr_FR) == 'nplurals=2; plural=(n > 1);' assert plural_fr_FR.num_plurals == 2 assert plural_fr_FR.plural_expr == '(n > 1)' plural_pl_PL = plurals.get_plural('pl_PL') assert str(plural_pl_PL) == 'nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);' assert plural_pl_PL.num_plurals == 3 assert plural_pl_PL.plural_expr == '(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'
def plural_expr(self): """The plural expression used by the catalog or locale. >>> Catalog(locale='en').plural_expr '(n != 1)' >>> Catalog(locale='ga').plural_expr '(n==1 ? 0 : n==2 ? 1 : 2)' :type: `string_types`""" if self._plural_expr is None: expr = '(n != 1)' if self.locale: expr = get_plural(self.locale)[1] self._plural_expr = expr return self._plural_expr
def num_plurals(self): """The number of plurals used by the catalog or locale. >>> Catalog(locale='en').num_plurals 2 >>> Catalog(locale='ga').num_plurals 3 :type: `int`""" if self._num_plurals is None: num = 2 if self.locale: num = get_plural(self.locale)[0] self._num_plurals = num return self._num_plurals
def plural_expr(self): """The plural expression used by the catalog or locale. >>> Catalog(locale='en').plural_expr '(n != 1)' >>> Catalog(locale='ga').plural_expr '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)' >>> Catalog(locale='ding').plural_expr # unknown locale '(n != 1)' :type: `string_types`""" if self._plural_expr is None: expr = '(n != 1)' if self.locale: expr = get_plural(self.locale)[1] self._plural_expr = expr return self._plural_expr
def add_locale(self, req): data = {} errors = [] catalog_template_id = req.args.get('catalog_template', None) locale = req.args.get('locale') locale_admins = req.args.getlist('admins') def add_error(error): errors.append(error) data['error'] = tag.ul(*[tag.li(e) for e in errors if e]) data['locale'] = locale return data if not catalog_template_id: errors.append(_("You must first create a catalog template")) if not locale: return add_error(_("You must define the new catalog's locale")) if not locale_admins: return add_error(_("You must define at least one locale admin")) Session = session(self.env) catalog = Session.query(Catalog).get(catalog_template_id) num_plurals = get_plural(locale=locale).num_plurals _locale = Session.query(Locale).filter_by(locale=locale, catalog_id=catalog.id).first() if _locale: data['locale'] = _locale return add_error(_("Locale Exists Already")) locale = Locale(catalog, locale, num_plurals) for sid in locale_admins: locale.admins.append(LocaleAdmin(locale, sid)) catalog.locales.append(locale) Session.commit() perm = PermissionSystem(self.env) sids_without_necessary_perms = [] for admin in locale.admins: if not 'L10N_MODERATE' in perm.get_user_permissions(admin.sid): sids_without_necessary_perms.append(admin.sid) if sids_without_necessary_perms: msg = ngettext( "%s does not have the required permissions to administrate." % \ ', '.join(["'%s'" % s for s in sids_without_necessary_perms]), "%s don't have the required permissions to administrate." % \ ', '.join(["'%s'" % s for s in sids_without_necessary_perms]), len(sids_without_necessary_perms)) add_error(tag(msg, _(" Don't forget to "), tag.a(_('update permissions'), href=req.href.admin('general', 'perm')), '.')) add_notice(req, _("Locale added.")) # Are we importing existing data locale_catalog_path = req.args.get('catalog') include_fuzzy = req.args.get('include_fuzzy') == '1' if not locale_catalog_path or locale_catalog_path == '/': return data repos = self.env.get_repository(req.authname) revision = repos.youngest_rev try: node = get_existing_node(req, repos, locale_catalog_path, revision) except NoSuchChangeset, e: raise ResourceNotFound(e.message, _('Invalid Changeset Number'))
def test_get_plural_accpets_strings(): assert plurals.get_plural(locale='ga') == (3, '(n==1 ? 0 : n==2 ? 1 : 2)')
def test_get_plural_falls_back_to_default(): assert plurals.get_plural('ii') == (2, '(n != 1)')
def test_get_plural_accpets_strings(): assert plurals.get_plural(locale='ga') == (5, '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)')
def test_get_plural_selection(locale, num_plurals, plural_expr): assert plurals.get_plural(locale) == (num_plurals, plural_expr)
def __init__(self, fp, locale): if not isinstance(locale, Locale): locale = Locale.parse(locale) self.locale = locale super(POTranslations, self).__init__(fp) self.plural = c2py(get_plural(locale).plural_expr)
def test_plural_tuple_attributes(): tup = plurals.get_plural("ja") assert tup.num_plurals == 1 assert tup.plural_expr == '0' assert tup.plural_forms == 'npurals=1; plural=0' assert str(tup) == 'npurals=1; plural=0'
def test_get_plural_accpets_strings(): assert plurals.get_plural(locale='ga') == ( 5, '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)')