From: Michael Birtwell Date: Wed, 30 Sep 2015 17:57:30 +0000 (+0100) Subject: Add an ordinal_form property to Locale X-Git-Tag: 2.2.0~11^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F270%2Fhead;p=thirdparty%2Fbabel.git Add an ordinal_form property to Locale --- diff --git a/babel/core.py b/babel/core.py index 7d242571..0b314cd3 100644 --- a/babel/core.py +++ b/babel/core.py @@ -756,6 +756,23 @@ class Locale(object): """ return self._data['list_patterns'] + @property + def ordinal_form(self): + """Plural rules for the locale. + + >>> Locale('en').ordinal_form(1) + 'one' + >>> Locale('en').ordinal_form(2) + 'two' + >>> Locale('en').ordinal_form(3) + 'few' + >>> Locale('fr').ordinal_form(2) + 'other' + >>> Locale('ru').ordinal_form(100) + 'other' + """ + return self._data.get('ordinal_form', _default_plural_rule) + def default_locale(category=None, aliases=LOCALE_ALIASES): """Returns the system default locale for a given category, based on diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 68bf454b..d179604c 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -109,6 +109,19 @@ def _currency_sort_key(tup): return int(not tender), start or (1, 1, 1) +def _extract_plural_rules(file_path): + rule_dict = {} + prsup = parse(file_path) + for elem in prsup.findall('.//plurals/pluralRules'): + rules = [] + for rule in elem.findall('pluralRule'): + rules.append((rule.attrib['count'], text_type(rule.text))) + pr = PluralRule(rules) + for locale in elem.attrib['locales'].split(): + rule_dict[locale] = pr + return rule_dict + + def main(): parser = OptionParser(usage='%prog path/to/cldr') options, args = parser.parse_args() @@ -260,15 +273,8 @@ def main(): containers.add(group) # prepare the per-locale plural rules definitions - plural_rules = {} - prsup = parse(os.path.join(srcdir, 'supplemental', 'plurals.xml')) - for elem in prsup.findall('.//plurals/pluralRules'): - rules = [] - for rule in elem.findall('pluralRule'): - rules.append((rule.attrib['count'], text_type(rule.text))) - pr = PluralRule(rules) - for locale in elem.attrib['locales'].split(): - plural_rules[locale] = pr + plural_rules = _extract_plural_rules(os.path.join(srcdir, 'supplemental', 'plurals.xml')) + ordinal_rules = _extract_plural_rules(os.path.join(srcdir, 'supplemental', 'ordinals.xml')) filenames = os.listdir(os.path.join(srcdir, 'main')) filenames.remove('root.xml') @@ -312,6 +318,8 @@ def main(): ])) if locale_id in plural_rules: data['plural_form'] = plural_rules[locale_id] + if locale_id in ordinal_rules: + data['ordinal_form'] = ordinal_rules[locale_id] #