changeset 387:88e3589ca8df

Improve CLDR import of currency-related data to ignore unsupported features such as symbol choice patterns and pluralized display names. See #93.
author cmlenz
date Fri, 11 Jul 2008 13:47:28 +0000
parents e8a81ad0f8d3
children ddb226bf0728
files babel/numbers.py scripts/dump_data.py scripts/import_cldr.py
diffstat 3 files changed, 30 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/babel/numbers.py
+++ b/babel/numbers.py
@@ -40,6 +40,20 @@
 
 LC_NUMERIC = default_locale('LC_NUMERIC')
 
+def get_currency_name(currency, locale=LC_NUMERIC):
+    """Return the name used by the locale for the specified currency.
+    
+    >>> get_currency_name('USD', 'en_US')
+    u'US Dollar'
+    
+    :param currency: the currency code
+    :param locale: the `Locale` object or locale identifier
+    :return: the currency symbol
+    :rtype: `unicode`
+    :since: version 0.9.4
+    """
+    return Locale.parse(locale).currencies.get(currency, currency)
+
 def get_currency_symbol(currency, locale=LC_NUMERIC):
     """Return the symbol used by the locale for the specified currency.
     
--- a/scripts/dump_data.py
+++ b/scripts/dump_data.py
@@ -36,7 +36,9 @@
     if len(args) > 1:
         for key in args[1].split('.'):
             data = data[key]
-    pprint(dict(data.items()))
+    if isinstance(data, dict):
+        data = dict(data.items())
+    pprint(data)
 
 
 if __name__ == '__main__':
--- a/scripts/import_cldr.py
+++ b/scripts/import_cldr.py
@@ -137,13 +137,11 @@
     filenames.insert(0, 'root.xml')
 
     for filename in filenames:
-        print>>sys.stderr, 'Processing input file %r' % filename
         stem, ext = os.path.splitext(filename)
         if ext != '.xml':
             continue
-        #if stem != 'root':
-        #    break
 
+        print>>sys.stderr, 'Processing input file %r' % filename
         tree = parse(os.path.join(srcdir, 'main', filename))
         data = {}
 
@@ -442,12 +440,18 @@
         currency_names = data.setdefault('currency_names', {})
         currency_symbols = data.setdefault('currency_symbols', {})
         for elem in tree.findall('//currencies/currency'):
-            name = elem.findtext('displayName')
-            if name:
-                currency_names[elem.attrib['type']] = unicode(name)
-            symbol = elem.findtext('symbol')
-            if symbol:
-                currency_symbols[elem.attrib['type']] = unicode(symbol)
+            code = elem.attrib['type']
+            # TODO: support plural rules for currency name selection
+            for name in elem.findall('displayName'):
+                if ('draft' in name.attrib or 'count' in name.attrib) \
+                        and code in currency_names:
+                    continue
+                currency_names[code] = unicode(name.text)
+            # TODO: support choice patterns for currency symbol selection
+            symbol = elem.find('symbol')
+            if symbol is not None and 'draft' not in symbol.attrib \
+                    and 'choice' not in symbol.attrib:
+                currency_symbols[code] = unicode(symbol.text)
 
         outfile = open(os.path.join(destdir, 'localedata', stem + '.dat'), 'wb')
         try:
Copyright (C) 2012-2017 Edgewall Software