Internationalization and Localization

This module defines API for i18n and L10n support.

Utilities

kalapy.i18n.utils.TRANSLATIONS

cache of loaded translations (speedup)

kalapy.i18n.utils.get_locale()

Returns the locale to be used for current request as babel.Locale object. If used outside of a request, it return default locale as specified by settings.DEFAULT_LOCALE.

kalapy.i18n.utils.get_timezone()

Returns the timezone to be used for current request as pytz.timezone object. If used outside of a request, it return default timezone as specified by settings.DEFAULT_TIMEZONE.

kalapy.i18n.utils.get_translations()

Returns gettext translations to be used for current locale.

kalapy.i18n.utils.gettext(string, **kwargs)

Translates the given string with current locale and passes the given keyword variables as a mapping to format the string. The returned value is a lazy instance which will be translated when it is actually used.

Example:

h1 = gettext('Hello World!')
h2 = gettext('Hello %(name)s!', name='World')

@web.route('/say')
def say():
    return h1
Parameters:
  • string – the string to be translated
  • kwargs – mapping to the format place holders
Returns:

a lazy instance to delay actual translation, translation will be performed when result is actually used.

kalapy.i18n.utils.load_translations(import_name, locale)

Loads gettext translations for the given locale from the specified package represented by the given import name.

kalapy.i18n.utils.ngettext(string, plural, num, **kwargs)

Translates the given string with current locale and passes the given keyword variables as a mapping to format the string.

It does a plural-forms lookup of a given string depending on the num and uses plural instead of string if num represents plural in the current locale.

The returned value is a lazy instance which will be translated when it is actually used.

Example:

ngettext('%(num)d Apple', '%(num)d Apples!', num=len(apples))
Parameters:
  • string – the string to be translated, singular form
  • plural – the plural form of the string
  • num – value of num placeholder
  • kwargs – mapping to the format place holders
Returns:

a lazy instance to delay actual translation, translation will be performed when result is actually used.

Formating

kalapy.i18n.format.format_date(date=None, format='medium')

Returns a date formated according to the given format in the context of current locale. If date is None, current date is assumed.

Parameters:
  • date – an instance of date or None
  • format – format pattern, one of short, medium, long or full.
Returns:

formated date as string

kalapy.i18n.format.format_datetime(datetime=None, format='medium')

Returns a datetime formated according to the given format in the context of current locale. If datetime is None, current time is assumed.

Parameters:
  • datetime – an instance of datetime or None
  • format – format pattern, one of short, medium, long or full.
Returns:

formated datetime as string

kalapy.i18n.format.format_decimal(decimal, digits=2)

Returns a formatted decimal value in the context of current locale. The appropriate thousands grouping and the decimal separator are used according to the current locale.

For example:

>>> format_decimal(12345.4321, digits=2)
... '12,345.43'
>>> format_decimal(Decimal('12345.4321'), digits=2)
... '12,345.43'
Parameters:
  • decimal – a float or Decimal value
  • digits – number of digits to the right of decimal seperator
kalapy.i18n.format.format_number(number)

Returns a formatted decimal value in the context of current locale.

Parameters:number – an integer value
kalapy.i18n.format.format_time(time=None, format='medium')

Returns a time formated according to the given format in the context of current locale. If time is None, current time is assumed.

Parameters:
  • time – an instance of time or None
  • format – format pattern, one of short, medium, long or full.
Returns:

formated time as string

kalapy.i18n.format.parse_date(string)

Parse a date from a string.

kalapy.i18n.format.parse_datetime(string)

Parse a datetime from a string.

kalapy.i18n.format.parse_decimal(string, force_decimal=False)

Parse a localized decimal string into a float if force_decimal is False, else into a real decimal.Decimal value.

Parameters:
  • string – localized decimal string value
  • force_decimal – whether to return Decimal instead of float
kalapy.i18n.format.parse_number(string)

Parse a localized number string into a long integer.

kalapy.i18n.format.parse_time(string)

Parse a time from a string.

Table Of Contents

Previous topic

Web Components

Next topic

Signals

This Page