def test_gettext_compilation(locale): # Test that new plural form elements introduced in recent CLDR versions # are compiled "down" to `n` when emitting Gettext rules. ru_rules = localedata.load(locale)['plural_form'].rules chars = 'ivwft' # Test that these rules are valid for this test; i.e. that they contain at least one # of the gettext-unsupported characters. assert any((" " + ch + " ") in rule for ch in chars for rule in ru_rules.values()) # Then test that the generated value indeed does not contain these. ru_rules_gettext = plural.to_gettext(ru_rules) assert not any(ch in ru_rules_gettext for ch in chars)
def test_plural_within_rules(): p = plural.PluralRule({'one': 'n is 1', 'few': 'n within 2,4,7..9'}) assert repr(p) == "<PluralRule 'one: n is 1, few: n within 2,4,7..9'>" assert plural.to_javascript(p) == ( "(function(n) { " "return ((n == 2) || (n == 4) || (n >= 7 && n <= 9))" " ? 'few' : (n == 1) ? 'one' : 'other'; })") assert plural.to_gettext(p) == ( 'nplurals=3; plural=(((n == 2) || (n == 4) || (n >= 7 && n <= 9))' ' ? 1 : (n == 1) ? 0 : 2)') assert p(0) == 'other' assert p(1) == 'one' assert p(2) == 'few' assert p(3) == 'other' assert p(4) == 'few' assert p(5) == 'other' assert p(6) == 'other' assert p(7) == 'few' assert p(8) == 'few' assert p(9) == 'few'
def get_cldr_languages(self): """ Helper function to extract CLDR information. Heavily relied on babel functionality """ from babel import Locale, localedata, plural for lang in localedata.locale_identifiers(): locale = Locale(lang) if not locale.english_name: continue plurals_str = plural.to_gettext(locale.plural_form) nplurals, pluralequation = re_plurals.match(plurals_str).groups() lang_aliases = set(CLDR_FALLBACK_ALIASES.get(lang, [])) lang_aliases.add(lang) for alias in lang_aliases: yield { 'code': alias, 'fullname': locale.english_name, 'nplurals': int(nplurals), 'pluralequation': pluralequation, }
def test_to_gettext(): assert (plural.to_gettext({ 'one': 'n is 1', 'two': 'n is 2' }) == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2)')
def test_to_gettext(): assert (plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'}) == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2)')