babelwrap module
Permits setting a default language for _()
and babel functions.
setlang()
- babelwrap.setlang(*langs: str) babel.core.Locale [source]
Gets/sets locale for language functions. E.g.:
setlang() # to get the currenty set locale setlang("zh_Hans_HK", "zh_HK") # to set a language (e.g. for testing) setlang("") # to restore the default language
- Parameters
*langs (
str
) – locale names in order of preference.- Returns
The babel.core.Locale that is currently set.
The babel functions can then be used (defaulted to the set language) as follows:
print(format_decimal(-12345.6789)) print(format_percent(-12345.6789)) print(format_unit(-12345.6789, "second")) print(format_datetime(datetime.datetime.now())) print(format_list(["Alvin", "Simon", "Theodore"])) print(_("Hello world!"))
format_datetime
- babelwrap.format_datetime = functools.partial(<function format_datetime>, locale=Locale('en'))
Default locale from setlang() Otherwise: Return a date formatted according to the given pattern.
>>> from datetime import datetime >>> dt = datetime(2007, 4, 1, 15, 30) >>> format_datetime(dt, locale='en_US') u'Apr 1, 2007, 3:30:00\u202fPM'
For any pattern requiring the display of the timezone:
>>> format_datetime(dt, 'full', tzinfo=get_timezone('Europe/Paris'), ... locale='fr_FR') 'dimanche 1 avril 2007, 17:30:00 heure d’été d’Europe centrale' >>> format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz", ... tzinfo=get_timezone('US/Eastern'), locale='en') u'2007.04.01 AD at 11:30:00 EDT'
- param datetime
the datetime object; if None, the current date and time is used
- param format
one of “full”, “long”, “medium”, or “short”, or a custom date/time pattern
- param tzinfo
the timezone to apply to the time for display
- param locale
a Locale object or a locale identifier
format_decimal
- babelwrap.format_decimal = functools.partial(<function format_decimal>, locale=Locale('en'))
Default locale from setlang() Otherwise: Return the given decimal number formatted for a specific locale.
>>> format_decimal(1.2345, locale='en_US') u'1.234' >>> format_decimal(1.2346, locale='en_US') u'1.235' >>> format_decimal(-1.2346, locale='en_US') u'-1.235' >>> format_decimal(1.2345, locale='sv_SE') u'1,234' >>> format_decimal(1.2345, locale='de') u'1,234'
The appropriate thousands grouping and the decimal separator are used for each locale:
>>> format_decimal(12345.5, locale='en_US') u'12,345.5'
By default the locale is allowed to truncate and round a high-precision number by forcing its format pattern onto the decimal part. You can bypass this behavior with the decimal_quantization parameter:
>>> format_decimal(1.2346, locale='en_US') u'1.235' >>> format_decimal(1.2346, locale='en_US', decimal_quantization=False) u'1.2346' >>> format_decimal(12345.67, locale='fr_CA', group_separator=False) u'12345,67' >>> format_decimal(12345.67, locale='en_US', group_separator=True) u'12,345.67'
- param number
the number to format
- param format
- param locale
the Locale object or locale identifier
- param decimal_quantization
Truncate and round high-precision numbers to the format pattern. Defaults to True.
- param group_separator
Boolean to switch group separator on/off in a locale’s number format.
format_list
- babelwrap.format_list = functools.partial(<function format_list>, locale=Locale('en'))
Default locale from setlang() Otherwise:
Format the items in lst as a list.
>>> format_list(['apples', 'oranges', 'pears'], locale='en') u'apples, oranges, and pears' >>> format_list(['apples', 'oranges', 'pears'], locale='zh') u'apples、oranges和pears' >>> format_list(['omena', 'peruna', 'aplari'], style='or', locale='fi') u'omena, peruna tai aplari'
These styles are defined, but not all are necessarily available in all locales. The following text is verbatim from the Unicode TR35-49 spec [1].
standard: A typical ‘and’ list for arbitrary placeholders. eg. “January, February, and March”
standard-short: A short version of a ‘and’ list, suitable for use with short or abbreviated placeholder values. eg. “Jan., Feb., and Mar.”
or: A typical ‘or’ list for arbitrary placeholders. eg. “January, February, or March”
or-short: A short version of an ‘or’ list. eg. “Jan., Feb., or Mar.”
unit: A list suitable for wide units. eg. “3 feet, 7 inches”
unit-short: A list suitable for short units eg. “3 ft, 7 in”
unit-narrow: A list suitable for narrow units, where space on the screen is very limited. eg. “3′ 7″”
[1]: https://www.unicode.org/reports/tr35/tr35-49/tr35-general.html#ListPatterns
- Parameters
lst – a sequence of items to format in to a list
style – the style to format the list with. See above for description.
locale – the locale
format_percent
- babelwrap.format_percent = functools.partial(<function format_percent>, locale=Locale('en'))
Default locale from setlang() Otherwise: Return formatted percent value for a specific locale.
>>> format_percent(0.34, locale='en_US') u'34%' >>> format_percent(25.1234, locale='en_US') u'2,512%' >>> format_percent(25.1234, locale='sv_SE') u'2\xa0512\xa0%'
The format pattern can also be specified explicitly:
>>> format_percent(25.1234, u'#,##0‰', locale='en_US') u'25,123‰'
By default the locale is allowed to truncate and round a high-precision number by forcing its format pattern onto the decimal part. You can bypass this behavior with the decimal_quantization parameter:
>>> format_percent(23.9876, locale='en_US') u'2,399%' >>> format_percent(23.9876, locale='en_US', decimal_quantization=False) u'2,398.76%'
>>> format_percent(229291.1234, locale='pt_BR', group_separator=False) u'22929112%'
>>> format_percent(229291.1234, locale='pt_BR', group_separator=True) u'22.929.112%'
- param number
the percent number to format
- param format
- param locale
the Locale object or locale identifier
- param decimal_quantization
Truncate and round high-precision numbers to the format pattern. Defaults to True.
- param group_separator
Boolean to switch group separator on/off in a locale’s number format.
format_unit
- babelwrap.format_unit = functools.partial(<function format_unit>, locale=Locale('en'))
Default locale from setlang() Otherwise: Format a value of a given unit.
Values are formatted according to the locale’s usual pluralization rules and number formats.
>>> format_unit(12, 'length-meter', locale='ro_RO') u'12 metri' >>> format_unit(15.5, 'length-mile', locale='fi_FI') u'15,5 mailia' >>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb') u'1\xa0200 millimeter kvikks\xf8lv' >>> format_unit(270, 'ton', locale='en') u'270 tons'
Number formats may be overridden with the
format
parameter.>>> import decimal >>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr') u'-42,8\u202f\xb0C'
The locale’s usual pluralization rules are respected.
>>> format_unit(1, 'length-meter', locale='ro_RO') u'1 metru' >>> format_unit(0, 'length-mile', locale='cy') u'0 mi' >>> format_unit(1, 'length-mile', locale='cy') u'1 filltir' >>> format_unit(3, 'length-mile', locale='cy') u'3 milltir'
>>> format_unit(15, 'length-horse', locale='fi') Traceback (most recent call last): ... UnknownUnitError: length-horse is not a known unit in fi
New in version 2.2.0.
- param value
the value to format. If this is a string, no number formatting will be attempted.
- param measurement_unit
the code of a measurement unit. Known units can be found in the CLDR Unit Validity XML file: https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
- param length
“short”, “long” or “narrow”
- param format
An optional format, as accepted by format_decimal.
- param locale
the Locale object or locale identifier