locale — 本地化API
优质
小牛编辑
134浏览
2023-12-01
Probing the Current Locale
# locale_env.py
import locale
import os
import pprint
# Default settings based on the user's environment.
locale.setlocale(locale.LC_ALL, '')
print('Environment settings:')
for env_name in ['LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE']:
print(' {} = {}'.format(
env_name, os.environ.get(env_name, ''))
)
# What is the locale?
print('\nLocale from environment:', locale.getlocale())
template = """
Numeric formatting:
Decimal point : "{decimal_point}"
Grouping positions : {grouping}
Thousands separator: "{thousands_sep}"
Monetary formatting:
International currency symbol : "{int_curr_symbol!r}"
Local currency symbol : {currency_symbol!r}
Symbol precedes positive value : {p_cs_precedes}
Symbol precedes negative value : {n_cs_precedes}
Decimal point : "{mon_decimal_point}"
Digits in fractional values : {frac_digits}
Digits in fractional values,
international : {int_frac_digits}
Grouping positions : {mon_grouping}
Thousands separator : "{mon_thousands_sep}"
Positive sign : "{positive_sign}"
Positive sign position : {p_sign_posn}
Negative sign : "{negative_sign}"
Negative sign position : {n_sign_posn}
"""
sign_positions = {
0: 'Surrounded by parentheses',
1: 'Before value and symbol',
2: 'After value and symbol',
3: 'Before value',
4: 'After value',
locale.CHAR_MAX: 'Unspecified',
}
info = {}
info.update(locale.localeconv())
info['p_sign_posn'] = sign_positions[info['p_sign_posn']]
info['n_sign_posn'] = sign_positions[info['n_sign_posn']]
print(template.format(**info))
Currency
# locale_currency.py
import locale
sample_locales = [
('USA', 'en_US'),
('France', 'fr_FR'),
('Spain', 'es_ES'),
('Portugal', 'pt_PT'),
('Poland', 'pl_PL'),
]
for name, loc in sample_locales:
locale.setlocale(locale.LC_ALL, loc)
print('{:>10}: {:>10} {:>10}'.format(
name,
locale.currency(1234.56),
locale.currency(-1234.56),
))
Formatting Numbers
# locale_grouping.py
import locale
sample_locales = [
('USA', 'en_US'),
('France', 'fr_FR'),
('Spain', 'es_ES'),
('Portugal', 'pt_PT'),
('Poland', 'pl_PL'),
]
print('{:>10} {:>10} {:>15}'.format(
'Locale', 'Integer', 'Float')
)
for name, loc in sample_locales:
locale.setlocale(locale.LC_ALL, loc)
print('{:>10}'.format(name), end=' ')
print(locale.format('%10d', 123456, grouping=True), end=' ')
print(locale.format('%15.2f', 123456.78, grouping=True))
# locale_delocalize.py
import locale
sample_locales = [
('USA', 'en_US'),
('France', 'fr_FR'),
('Spain', 'es_ES'),
('Portugal', 'pt_PT'),
('Poland', 'pl_PL'),
]
for name, loc in sample_locales:
locale.setlocale(locale.LC_ALL, loc)
localized = locale.format('%0.2f', 123456.78, grouping=True)
delocalized = locale.delocalize(localized)
print('{:>10}: {:>10} {:>10}'.format(
name,
localized,
delocalized,
))
Parsing Numbers
# locale_atof.py
import locale
sample_data = [
('USA', 'en_US', '1,234.56'),
('France', 'fr_FR', '1234,56'),
('Spain', 'es_ES', '1234,56'),
('Portugal', 'pt_PT', '1234.56'),
('Poland', 'pl_PL', '1 234,56'),
]
for name, loc, a in sample_data:
locale.setlocale(locale.LC_ALL, loc)
print('{:>10}: {:>9} => {:f}'.format(
name,
a,
locale.atof(a),
))
Dates and Times
# locale_date.py
import locale
import time
sample_locales = [
('USA', 'en_US'),
('France', 'fr_FR'),
('Spain', 'es_ES'),
('Portugal', 'pt_PT'),
('Poland', 'pl_PL'),
]
for name, loc in sample_locales:
locale.setlocale(locale.LC_ALL, loc)
format = locale.nl_langinfo(locale.D_T_FMT)
print('{:>10}: {}'.format(name, time.strftime(format)))