Python: Locix Access Posix database without setlocale ()

Customization is a Django-based site on a Ubuntu server system with lots of useful information in /usr/share/i18n/locales.

Question: Can I access this pool of wisdom without using Python locale.setlocale()before?

Reason: Documents say it

  • very expensive to call setlocale()and

  • affects the entire application.

But in my case, I have, say, a French site (Django automatically sets the locale automatically), and I just want to display the name of January in the locale de_ATor format the number, as in Russia.

+3
source share
1 answer

The magic library to achieve this is called Babel . Does what I want:

Before

import locale
setlocale(LC_ALL, 'de')
x = locale.format('%.2f', 123)
setlocale(LC_ALL, '')

After

from babel.numbers import format_decimal
x = format_decimal(123, format='#0.00', locale='de')

... and has good Djang integration.

+3
source

Source: https://habr.com/ru/post/1735010/


All Articles