python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.34k stars 448 forks source link

Inconsistent `UnknownLocaleError` #1118

Closed SirRob1997 closed 3 months ago

SirRob1997 commented 3 months ago

There seems to be inconsistency when UnknownLocaleError is thrown versus when None is returned. See below example:

>>> from babel import Locale
>>> print(Locale('mr_IN').get_language_name('en_US'))
None
>>> Locale('mr').get_language_name('en_US')
'Marathi'
>>> from babel import Locale
>>> print(Locale('pt_ES').get_language_name('en_US'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/envs/poetry/lib/python3.9/site-packages/babel/core.py", line 204, in __init__
    raise UnknownLocaleError(identifier)
babel.core.UnknownLocaleError: unknown locale 'pt_ES'

>>> Locale('pt').get_language_name('en_US')
'Portuguese'

Both mr_IN and pt_ES can't be found while their respective Locale exists for the language itself i.e. mr or pt so I'd expect both of them to either return None or both of them to throw an UnknownLocaleError.