What Is the Correct Way to Set Python's Locale on Windows

What is the correct way to set Python's locale on Windows?

It seems you're using Windows. The locale strings are different there. Take a more precise look at the doc:

locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform

On Windows, I think it would be something like:

locale.setlocale(locale.LC_ALL, 'deu_deu')

MSDN has a list of language strings and of country/region strings

How to change locale in python in windows?

You can take a look at the pycountry library to have a mapping between Windows and Linux language codes:

>>> pycountry.languages.lookup('fr')
Language(alpha_2=u'fr', alpha_3=u'fra', bibliographic=u'fre', name=u'French', scope=u'I', type=u'L')
>>> pycountry.languages.lookup('french')
Language(alpha_2=u'fr', alpha_3=u'fra', bibliographic=u'fre', name=u'French', scope=u'I', type=u'L')
>>> pycountry.languages.lookup('chinese')
Language(alpha_2=u'zh', alpha_3=u'zho', bibliographic=u'chi', name=u'Chinese', scope=u'M', type=u'L')
>>> pycountry.languages.lookup('chinese-traditional')
Traceback (most recent call last):
...
LookupError: Could not find a record for 'chinese-traditional'

Then you can do:

import os
import locale
import pycountry

lang = "en_IN" # your code
language = pycountry.languages.lookup(lang)
if os.name == "posix":
locale.setlocale(locale.LC_MONETARY, language.alpha_2)
else:
locale.setlocale(locale.LC_MONETARY, language.name)

EDIT

To format currency values, you may consider using Babel, for instance:

>>> babel.numbers.format_currency(10000, 'INR', locale='en_IN')
u'\u20b9\xa010,000.00'

>>> print(babel.numbers.format_currency(10000, 'INR', locale='en_IN'))
₹ 10,000.00

Python - How to set French locale?

add this in RobotFramework (at the beginning):

${osName}=    Evaluate    platform.system()    platform
Run keyword if "${osName}"=='Windows' Evaluate locale.setlocale(locale.LC_ALL, 'french') locale
... ELSE Evaluate locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8') locale

How to select Japanese locale on Windows?

If you're using windows, this doesn't work because the japanese locale is 'jpn'. Try:

locale.setlocale(locale.LC_COLLATE, 'jpn')

Here is a list of country/region strings supported.

Setting Python locale doesn't work

It seems like nothing is saved. Am I wrong in assuming you set your locale once and then the system will remember this

yes, calling locale.setlocale() in Python does not affect future python processes. Configure environment variables instead, see How to set all locale settings in Ubuntu.

Bash's "date" method seems to pick up the locale some way or the other.

date calls setlocale(LC_ALL, "") at the start i.e., you need to call setlocale() at least once per process to enable $LANG locale instead of C locale.


setlocale(LC_ALL, '') sets locale according to $LANG variable first, not $LANGUAGE (it is related but different: "The GNU gettext search path contains 'LC_ALL', 'LC_CTYPE', 'LANG' and 'LANGUAGE', in that order.").

It is enough to set LC_TIME category (on Ubuntu):

>>> import locale
>>> import time
>>> time.strftime('%A')
'Tuesday'
>>> locale.getlocale(locale.LC_TIME)
('en_US', 'UTF-8')
>>> locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8')
'ru_RU.UTF-8'
>>> time.strftime('%A')
'Вторник'
>>> locale.getlocale(locale.LC_TIME)
('ru_RU', 'UTF-8')

If setlocale() hasn't raised locale.Error: unsupported locale setting then the corresponding locale category is set successfully.

You could also get the weekday name knowing its position (in the same python session where the locale is changed):

>>> import calendar
>>> calendar.day_name[1]
'Вторник'
>>> locale.nl_langinfo(locale.DAY_3)
'Вторник'

A portable way, to print a weekday in a given locale without modifying a global state, is to use babel module:

>>> from datetime import date
>>> from babel.dates import format_date # $ pip install babel
>>> format_date(date.today(), format='EEEE', locale='en')
'Tuesday'
>>> format_date(date.today(), format='EEEE', locale='ru')
'вторник'
>>> format_date(date.today(), format='EEEE', locale='nl')
'dinsdag'

How can I list all available windows locales in python console?

>>> import locale
>>> locale.locale_alias

Windows Python: Changing encoding using the locale module

Check this https://docs.moodle.org/dev/Table_of_locales

I think in windows you need to set 'localewin' value instead of the locale name. Setting locale.setlocale( locale.LC_ALL, 'English_United States.1252' ) worked for me in windows. I also tried setting different locales Dutch_Netherlands.1252 and they worked. Though this might not solve your problem of UnicodeEncodeError, but I think this atleast explains why you are unable to set the locale.



Related Topics



Leave a reply



Submit