Python 3 Tutorial
Every Computer System has Locale Settings configured to your regions or country. The set of settings related to the language and region in which your computer program executes. Examples are language,, currency and time formats, character encoding etc.,
In Other wordsEvery Country has its own culture, The language they speak, Date formats,time formats, Currency, Telephone numbers, measurements (Miles,Kilo meters) etc.,
Locale module can help you to display in native formats.
Import locale moduleimport locale
I am using Linux , Fedora Operating System, and it is configured to Asia/Kolkata region, India has different Number System, Telephone Number format, Currency Symbol, Date Time formats etc., Measurements like Kilometers, liters,Kilograms etc., as shown below. Default Encoding is UTF-8 , means all Indian Language Characters require more than one byte
Type locale at command prompt$ localeThe following output displayed
LANG=en_IN.UTF-8 LC_CTYPE="en_IN.UTF-8" LC_NUMERIC="en_IN.UTF-8" LC_TIME="en_IN.UTF-8" LC_COLLATE="en_IN.UTF-8" LC_MONETARY="en_IN.UTF-8" LC_MESSAGES="en_IN.UTF-8" LC_PAPER="en_IN.UTF-8" LC_NAME="en_IN.UTF-8" LC_ADDRESS="en_IN.UTF-8" LC_TELEPHONE="en_IN.UTF-8" LC_MEASUREMENT="en_IN.UTF-8" LC_IDENTIFICATION="en_IN.UTF-8" LC_ALL=Using Python get locale settings
import locale print(locale.getlocale()[0]," ",locale.getlocale()[1]); en_IN UTF-8 #en_IN English India #Encoding UTF-8
locale module has method called getlocale, getdefaultlocale methods, which returns a tuple, first element is language the system is configured to, type of encoding system using
Display all locale settings>>> locale.locale_alias
locale_alias is a dictionary , key value pair,
Change locale settings according to your needslocale.setlocal(locale.LC_ALL,'en-IN') time.strftime("%c",time.time()) locale.setlocal(locale.LC_ALL,'Japanese') time.strftime("%c",time.time()) locale.setlocal(locale.LC_ALL,'German') time.strftime("%c",time.time())Currency Format for Different Locales
>>> locale.currency(100000,symbol=True,grouping=True,international=True) 'INR 1,00,000.00' >>> locale.currency(100000,symbol=False,grouping=True,international=True) '1,00,000.00' >>> locale.currency(100000) '₹100000.00' >>> locale.currency(100000,grouping=True) '₹1,00,000.00' >>> >>> locale.setlocale(locale.LC_ALL,'en_IN') 'en_IN' >>> locale.currency(100000,grouping=True) '₹1,00,000.00' >>> locale.setlocale(locale.LC_ALL,'en_US') 'en_US' >>> locale.currency(100000,grouping=True) '$100,000.00' >>> locale.setlocale(locale.LC_ALL,'en_GB') 'en_GB' >>> locale.currency(100000,grouping=True) '£100,000.00' >>> locale.setlocale(locale.LC_ALL,'Japanese') 'Japanese' >>> locale.currency(100000,grouping=True) '¥100,000' >>> locale.setlocale(locale.LC_ALL,'German') 'German' >>> locale.currency(100000,grouping=True) '100.000,00 EUR' >>>Thousand Seperateor for different Countrys
>>> locale.setlocale(locale.LC_ALL,'en_IN') 'en_IN' >>> locale.localize("100000",grouping=True,monetary=False) '1,00,000' >>> locale.setlocale(locale.LC_ALL,'en_US') 'en_US' >>> locale.localize("100000",grouping=True,monetary=False) '100,000' >>> locale.setlocale(locale.LC_ALL,'en_GB') 'en_GB' >>> locale.localize("100000",grouping=True,monetary=False) '100,000' >>>
ADS