Chapter XV: internationalization and localization -locale: cultural exploration of the current locale localization API-

15.2 locale: cultural localization API
locale module is part of the internationalization and localization support Python libraries. It provides a standard method, may depend on the language may be processed or an operation position of the user. For example, it will format numbers in currency, sort complete string comparison, and also the processing date. It does not involve conversion (see gettext module) or Unicode encoding (see codecs module).

Note: to change the locale may impact on the entire application, so the recommended practices is to avoid changing the value of the library, but to use one-time setup. In the examples in this section, the locale will change several times in a small program to emphasize the difference between different locale settings. Generally more common situation is when activated by the application provided a locale or a locale setting when receiving a Web request, and will not repeatedly change.

This section describes the locale module in some of the advanced functions. Other functions, compared with lower-level functions (format_string ()) or the localized environment-related management applications (resetlocale ()).

15.2.1 probe current locale
let the user change the locale settings, the most common way is through an environment variable (LC_ALL, LC_CTYPE, LANG or LANGUAGE, depending on which platform). Then the application calls setlocale () instead of specifying a hard-coded values, and also the use of environmental values.

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_TCYPE','LANG','LANGUAGE']:
    print('  {} = {}'.format(
        env_name,os.environ.get(env_name,''))
          )

# What is the locale?
print('\nLocale from environment:',locale.getlocale())

template = """
Numeric formattting:

  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))

localeconv () method returns a dictionary containing the agreed localized environment. Standard Library documents are given full value names and definitions.

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/94830489