summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Birtwell <michael.birtwell@starleaf.com>2015-09-30 18:57:30 +0100
committerMichael Birtwell <michael.birtwell@gmail.com>2015-10-31 14:27:00 +0000
commitb3f3430d2e77871fdca0b119adf0b22b9140ffa2 (patch)
tree36457f9a395b9927d9c54b5fe31e70a4069cf7d8
parent5116c167241d7e01870f17adf8de0a1c86744ea4 (diff)
downloadbabel-b3f3430d2e77871fdca0b119adf0b22b9140ffa2.tar.gz
Add an ordinal_form property to Locale
-rw-r--r--babel/core.py17
-rwxr-xr-xscripts/import_cldr.py26
2 files changed, 34 insertions, 9 deletions
diff --git a/babel/core.py b/babel/core.py
index 7d24257..0b314cd 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 68bf454..d179604 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]
# <localeDisplayNames>