Is there a way to temporarily activate a locale within the scope of a block of code? Basically, I want to do something like this:
locale.setlocale(locale.LC_NUMERIC, 'nl_BE.utf8')
Like this:
with override_locale(locale.LC_NUMERIC, 'nl_BE.utf8'):
# Stuff here
That way I can also avoid any thread safety issues that may arise when using setlocale
. My use case is parsing an uploaded file where the decimals use a comma instead of a period as the decimal separator (e.g. 1,25 instead of 1.25).
I found out that Babel better fits my use case:
>>> parse_decimal('1,25', locale='nl_BE.utf8')
Decimal('1.25')
This approach is useful whenever I need to parse a Dutch decimal and doesn't require overriding any locales at all.