# HG changeset patch # User cmlenz # Date 1215784048 0 # Node ID cd8761c6f1a6c0f37816b7ae5822002ace5bd605 # Parent c14986ac2151f4a1cd9574f5dda1f32bdda623e0 Improve CLDR import of currency-related data to ignore unsupported features such as symbol choice patterns and pluralized display names. See #93. diff --git a/babel/numbers.py b/babel/numbers.py --- 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. diff --git a/scripts/dump_data.py b/scripts/dump_data.py --- 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__': diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py --- 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: