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.

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, 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.

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.

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.