]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
numbers: Properly load and expose currency format types
authorIsaac Jurado <diptongo@gmail.com>
Sun, 16 Aug 2015 09:27:03 +0000 (11:27 +0200)
committerIsaac Jurado <diptongo@gmail.com>
Sun, 27 Sep 2015 13:05:10 +0000 (15:05 +0200)
The type of the currency format (e.g. "standard", "accounting") was not
interpreted correctly from the CLDR data.  Now there should not be any currency
format identified by "None".

Fixes https://github.com/mitsuhiko/babel/issues/201

babel/core.py
babel/numbers.py
scripts/import_cldr.py
tests/test_core.py

index 712585856028bd22fd752a0fb3edda8672748b50..43d3bc3233cf30bf506e1827a5a1d47fcd8598ef 100644 (file)
@@ -544,7 +544,9 @@ class Locale(object):
     def currency_formats(self):
         """Locale patterns for currency number formatting.
 
-        >>> print Locale('en', 'US').currency_formats[None]
+        >>> print Locale('en', 'US').currency_formats['standard']
+        <NumberPattern u'\\xa4#,##0.00'>
+        >>> print Locale('en', 'US').currency_formats['accounting']
         <NumberPattern u'\\xa4#,##0.00'>
         """
         return self._data['currency_formats']
index 19a376bea6d12d0935386e0930130767e5a57eb2..672fd226b274283dddc86cc9ee14c84dcce5461c 100644 (file)
@@ -256,7 +256,7 @@ def format_decimal(number, format=None, locale=LC_NUMERIC):
     return pattern.apply(number, locale)
 
 
-def format_currency(number, currency, format=None, locale=LC_NUMERIC, currency_digits=True):
+def format_currency(number, currency, format='standard', locale=LC_NUMERIC, currency_digits=True):
     u"""Return formatted currency value.
 
     >>> format_currency(1099.98, 'USD', locale='en_US')
@@ -298,9 +298,7 @@ def format_currency(number, currency, format=None, locale=LC_NUMERIC, currency_d
     :param currency_digits: use the currency's number of decimal digits
     """
     locale = Locale.parse(locale)
-    if not format:
-        format = locale.currency_formats.get(format)
-    pattern = parse_pattern(format)
+    pattern = parse_pattern(locale.currency_formats.get(format, format))
     if currency_digits:
         fractions = get_global('currency_fractions')
         try:
index 59dfeac6d73032d619304fed1ee7bbddb8eb8e32..b56d07789d2f12f0661f4ebe0ee9eec8336a985f 100755 (executable)
@@ -586,13 +586,20 @@ def main():
                 numbers.parse_pattern(pattern)
 
         currency_formats = data.setdefault('currency_formats', {})
-        for elem in tree.findall('.//currencyFormats/currencyFormatLength'):
+        for elem in tree.findall('.//currencyFormats/currencyFormatLength/currencyFormat'):
             if ('draft' in elem.attrib or 'alt' in elem.attrib) \
                     and elem.attrib.get('type') in currency_formats:
                 continue
-            pattern = text_type(elem.findtext('currencyFormat/pattern'))
-            currency_formats[elem.attrib.get('type')] = \
-                numbers.parse_pattern(pattern)
+            for child in elem.getiterator():
+                if child.tag == 'alias':
+                    currency_formats[elem.attrib.get('type')] = Alias(
+                        _translate_alias(['currency_formats', elem.attrib['type']],
+                                         child.attrib['path'])
+                    )
+                elif child.tag == 'pattern':
+                    pattern = text_type(child.text)
+                    currency_formats[elem.attrib.get('type')] = \
+                        numbers.parse_pattern(pattern)
 
         percent_formats = data.setdefault('percent_formats', {})
         for elem in tree.findall('.//percentFormats/percentFormatLength'):
index a643b36a9fceaab2d27c1529e1b3c19407310ce3..9d954819412bee6636952edaa2aa90db5979b175 100644 (file)
@@ -157,7 +157,9 @@ class TestLocaleClass:
         assert Locale('en', 'US').decimal_formats[None].pattern == '#,##0.###'
 
     def test_currency_formats_property(self):
-        assert (Locale('en', 'US').currency_formats[None].pattern ==
+        assert (Locale('en', 'US').currency_formats['standard'].pattern ==
+                u'\xa4#,##0.00')
+        assert (Locale('en', 'US').currency_formats['accounting'].pattern ==
                 u'\xa4#,##0.00')
 
     def test_percent_formats_property(self):