Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add symbol to currency code mapping #677

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,17 @@ def currencies(self):
"""
return self._data['currency_names']

@property
def currency_codes(self):
"""Mapping of currency symbols to codes.

>>> Locale('en', 'US').currency_codes['$']
u'USD'
>>> Locale('es', 'CO').currency_codes['US$']
u'USD'
"""
return self._data['currency_codes']

@property
def currency_symbols(self):
"""Mapping of currency codes to symbols.
Expand Down
12 changes: 12 additions & 0 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ def normalize_currency(currency, locale=None):
return currency


def get_currency_code(symbol, locale=LC_NUMERIC):
"""Returns the code used by the locale for the specified currency symbol.

>>> get_currency_code('$', locale='en_US')
u'USD'

:param symbol: the currency symbol.
:param locale: the `Locale` object or locale identifier.
"""
return Locale.parse(locale).currency_codes.get(symbol, symbol)


def get_currency_name(currency, count=None, locale=LC_NUMERIC):
"""Return the name used by the locale for the specified currency.

Expand Down
2 changes: 2 additions & 0 deletions scripts/import_cldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ def parse_currency_names(data, tree):
currency_names = data.setdefault('currency_names', {})
currency_names_plural = data.setdefault('currency_names_plural', {})
currency_symbols = data.setdefault('currency_symbols', {})
currency_codes = data.setdefault('currency_codes', {})
for elem in tree.findall('.//currencies/currency'):
code = elem.attrib['type']
for name in elem.findall('displayName'):
Expand All @@ -826,6 +827,7 @@ def parse_currency_names(data, tree):
if symbol.attrib.get('alt'): # Skip alternate forms
continue
currency_symbols[code] = text_type(symbol.text)
currency_codes[text_type(symbol.text)] = code


def parse_unit_patterns(data, tree):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def test_get_currency_symbol():
assert numbers.get_currency_symbol('USD', 'en_US') == u'$'


def test_get_currency_code():
assert numbers.get_currency_code('$', 'en_US') == u'USD'


def test_get_currency_precision():
assert get_currency_precision('EUR') == 2
assert get_currency_precision('JPY') == 0
Expand Down