gws.lib.intl

Intl and localization tools.

Source code: gws.lib.intl

Package Contents

class gws.lib.intl.DateFormatter(loc: gws.Locale)

Bases: gws.DateFormatter

Locale-aware date formatter.

iso

Returns the date in the ISO 8601 format 2013-12-11.

locale
long

Returns the date in the medium format 11. Dezember 2013.

medium

Returns the date in the medium format 11.12.2013.

short

Returns the date in the short format 11.12.13.

format(fmt: gws.DateTimeFormat, date=None)

Formats the date.

Parameters:
  • fmt – Format type or a strftime format string

  • date – Date, if none is given the current date will be used as default.

Returns:

A formatted date string.

gws.lib.intl.default_locale()

Returns the default locale object (en_CA).

gws.lib.intl.formatters(loc: gws.Locale) tuple[DateFormatter, TimeFormatter, NumberFormatter]

Return a tuple of locale-aware formatters.

gws.lib.intl.locale(name: str | None, allowed: list[str] = None, fallback: bool = True) gws.Locale

Locates a Locale object by locale name.

If the name is invalid, and fallback is True, return the first allowed locale, or the default locale. Otherwise, raise an exception.

Parameters:
  • name – Language or locale name like de or de_DE.

  • allowed – A list of allowed locale uids.

  • fallback – Fall back to the default locale.

class gws.lib.intl.NumberFormatter(loc: gws.Locale)

Bases: gws.NumberFormatter

Locale-aware number formatter.

fns
locale
currency(n, currency, *args, **kwargs)

Returns formatted currency value.

decimal(n, *args, **kwargs)

Returns formatted decimal value.

format(fmt, n, *args, **kwargs)

Formats the number with respect to the locale.

Parameters:
  • fmt – Format type or a python format string

  • n – Number.

  • kwargs – Passes the currency parameter forward.

Returns:

A formatted number.

grouped(n, *args, **kwargs)

Returns formatted decimal value with group separators.

percent(n, *args, **kwargs)

Returns formatted percent value.

class gws.lib.intl.TimeFormatter(loc: gws.Locale)

Bases: gws.TimeFormatter

Locale-aware time formatter.

iso

Returns the time and date in the ISO 8601 format.

locale
long

Returns the time in the medium format 11:22:33.

medium

Returns the time in the medium format 11:22:33.

short

Returns the time in the short format 11:22.

format(fmt: gws.DateTimeFormat, date=None) str

Formats the time.

Parameters:
  • fmt – Format type or a strftime format string

  • time – Time, if none is given the current time will be used as default.

Returns:

A formatted time string.