From df676ab8dbeabb48e2a60ff8da74fc3bee21a979 Mon Sep 17 00:00:00 2001 From: Isaac Jurado Date: Sun, 16 Aug 2015 11:27:03 +0200 Subject: [PATCH] numbers: Properly load and expose currency format types 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 | 4 +++- babel/numbers.py | 6 ++---- scripts/import_cldr.py | 15 +++++++++++---- tests/test_core.py | 4 +++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/babel/core.py b/babel/core.py index 71258585..43d3bc32 100644 --- a/babel/core.py +++ b/babel/core.py @@ -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'] + + >>> print Locale('en', 'US').currency_formats['accounting'] """ return self._data['currency_formats'] diff --git a/babel/numbers.py b/babel/numbers.py index 19a376be..672fd226 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -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: diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 59dfeac6..b56d0778 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -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'): diff --git a/tests/test_core.py b/tests/test_core.py index a643b36a..9d954819 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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): -- 2.47.2