Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Textdomain

A Textdomain is a container for an esgettext configuration and all loaded LocaleContainer for the textual domain selected.

The actual translation methods have quite funny names like _() or _x(). The purpose of this naming convention is to make the internationalization of your programs as little obtrusive as possible. Most of the times you just have to exchange

doSomething('Hello, world!');

with

doSomething(gtx._('Hello, world!'));

Besides, depending on the string extractor you are using, it may be useful that the method names do not collide with method names from other packages.

Hierarchy

  • Textdomain

Index

Properties

_

_: (msgid: string) => string

Retrieve a translation for a string.

param

the string to translate

returns

the translated string

Type declaration

    • (msgid: string): string
    • Parameters

      • msgid: string

      Returns string

_l

_l: (locale: string, msgid: string) => string

Retrieve a translation for a string with a fixed locale.

param

the locale identifier

param

the string to translate

returns

the translated string

Type declaration

    • (locale: string, msgid: string): string
    • Parameters

      • locale: string
      • msgid: string

      Returns string

_ln

_ln: (locale: string, msgid: string, msgidPlural: string, numItems: number) => string

Retrieve a translation for a string containing a possible plural with a fixed locale. You will almost always want to call _nx instead so that you can interpolate the number of items into the strings.

param

the locale identifier

param

the string in the singular

param

the string in the plural

param

the number of items

returns

the translated string

Type declaration

    • (locale: string, msgid: string, msgidPlural: string, numItems: number): string
    • Parameters

      • locale: string
      • msgid: string
      • msgidPlural: string
      • numItems: number

      Returns string

_lnp

_lnp: (locale: string, msgctxt: string, msgid: string, msgidPlural: string, numItems: number) => string

The method _lnp() combines _ln() with _lp(). You will almost always want to call _npx instead so that you can interpolate the number of items into the strings.

param

the locale identifier

param

the message context

param

the message id

param

a dictionary with placehoders

returns

the translated string

Type declaration

    • (locale: string, msgctxt: string, msgid: string, msgidPlural: string, numItems: number): string
    • Parameters

      • locale: string
      • msgctxt: string
      • msgid: string
      • msgidPlural: string
      • numItems: number

      Returns string

_lnpx

_lnpx: (locale: string, msgctxt: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders) => string

The method _lnpx() brings it all together. It combines _ln() and _lp()and_lx()`.

param

the locale identifier

param

the message context

param

the message id

param

the plural string

param

the number of items

param

an optional dictionary with placehoders

returns

the translated string

Type declaration

    • (locale: string, msgctxt: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders): string
    • Parameters

      • locale: string
      • msgctxt: string
      • msgid: string
      • msgidPlural: string
      • numItems: number
      • Optional placeholders: Placeholders

      Returns string

_lnx

_lnx: (locale: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders) => string

Translate a string with a plural expression with placeholders into a fixed locale.

param

the locale identifier

param

the string in the singular

param

the string in the plural

param

the number of items

param

an optional dictionary of placeholders

returns

the translated string

Type declaration

    • (locale: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders): string
    • Parameters

      • locale: string
      • msgid: string
      • msgidPlural: string
      • numItems: number
      • Optional placeholders: Placeholders

      Returns string

_lp

_lp: (locale: string, msgctxt: string, msgid: string) => string

Translate a string with a context with a fixed locale.

param

the locale identifier

param

the message context

param

the string to translate

returns

the translated string

Type declaration

    • (locale: string, msgctxt: string, msgid: string): string
    • Parameters

      • locale: string
      • msgctxt: string
      • msgid: string

      Returns string

_lpx

_lpx: (msgctxt: string, msgid: string, placeholders?: Placeholders) => string

The method _lpx() combines _lp() with _lx().

param

the locale identifier

param

the message context

param

the message id

param

an optional dictionary with placehoders

returns

the translated string

Type declaration

    • (msgctxt: string, msgid: string, placeholders?: Placeholders): string
    • Parameters

      • msgctxt: string
      • msgid: string
      • Optional placeholders: Placeholders

      Returns string

_lx

_lx: (locale: string, msgid: string, placeholders?: Placeholders) => string

Translate a string with placeholders for a fixed locale. The placeholders should be wrapped into curly braces and must match the regular expression "[_a-zA-Z][_a-zA-Z0-9]*".

param

the locale identifier

param

the msgid to translate

param

an optional dictionary of placeholders

returns

the translated string with placeholders expanded

Type declaration

    • (locale: string, msgid: string, placeholders?: Placeholders): string
    • Parameters

      • locale: string
      • msgid: string
      • Optional placeholders: Placeholders

      Returns string

_n

_n: (msgid: string, msgidPlural: string, numItems: number) => string

Retrieve a translation for a string containing a possible plural. You will almost always want to call _nx instead so that you can interpolate the number of items into the strings.

param

the string in the singular

param

the string in the plural

param

the number of items

returns

the translated string

Type declaration

    • (msgid: string, msgidPlural: string, numItems: number): string
    • Parameters

      • msgid: string
      • msgidPlural: string
      • numItems: number

      Returns string

_np

_np: (msgctxt: string, msgid: string, msgidPlural: string, numItems: number) => string

The method _np() combines _n() with _p(). You will almost always want to call _npx instead so that you can interpolate the number of items into the strings.

param

the message context

param

the message id

param

a dictionary with placehoders

returns

the translated string

Type declaration

    • (msgctxt: string, msgid: string, msgidPlural: string, numItems: number): string
    • Parameters

      • msgctxt: string
      • msgid: string
      • msgidPlural: string
      • numItems: number

      Returns string

_npx

_npx: (msgctxt: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders) => string

The method _npx() brings it all together. It combines _n() and _p()and_x()`.

param

the message context

param

the message id

param

the plural string

param

the number of items

param

an optional dictionary with placehoders

returns

the translated string

Type declaration

    • (msgctxt: string, msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders): string
    • Parameters

      • msgctxt: string
      • msgid: string
      • msgidPlural: string
      • numItems: number
      • Optional placeholders: Placeholders

      Returns string

_nx

_nx: (msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders) => string

Translate a string with a plural expression with placeholders.

param

the string in the singular

param

the string in the plural

param

the number of items

param

an optional dictionary of placeholders

returns

the translated string

Type declaration

    • (msgid: string, msgidPlural: string, numItems: number, placeholders?: Placeholders): string
    • Parameters

      • msgid: string
      • msgidPlural: string
      • numItems: number
      • Optional placeholders: Placeholders

      Returns string

_p

_p: (msgctxt: string, msgid: string) => string

Translate a string with a context.

param

the message context

param

the string to translate

returns

the translated string

Type declaration

    • (msgctxt: string, msgid: string): string
    • Parameters

      • msgctxt: string
      • msgid: string

      Returns string

_px

_px: (msgctxt: string, msgid: string, placeholders?: Placeholders) => string

The method _px() combines _p() with _x().

param

the message context

param

the message id

param

an optional dictionary with placehoders

returns

the translated string

Type declaration

    • (msgctxt: string, msgid: string, placeholders?: Placeholders): string
    • Parameters

      • msgctxt: string
      • msgid: string
      • Optional placeholders: Placeholders

      Returns string

_x

_x: (msgid: string, placeholders?: Placeholders) => string

Translate a string with placeholders. The placeholders should be wrapped into curly braces and must match the regular expression "[_a-zA-Z][_a-zA-Z0-9]*".

param

the msgid to translate

param

an optional dictionary of placeholders

returns

the translated string with placeholders expanded

Type declaration

    • (msgid: string, placeholders?: Placeholders): string
    • Parameters

      • msgid: string
      • Optional placeholders: Placeholders

      Returns string

getInstance

getInstance: (textdomain: string) => Textdomain

Instantiate a Textdomain object. Textdomain objects are singletons for each textdomain identifier.

param

the textdomain of your application or library.

Type declaration

Accessors

catalogFormat

  • get catalogFormat(): string
  • set catalogFormat(format: string): void
  • Get the catalog format in use.

    Returns string

    one of 'json' or 'mo' (default is 'json')

  • Set the catalog format to use.

    Parameters

    • format: string

      one of 'json' or 'mo'

    Returns void

    one of 'json' or 'mo' (default is 'json')

Static locale

  • get locale(): string
  • set locale(locale: string): void
  • Query the locale in use.

    For the web you can use all valid language identifier tags that BCP47 allows (and actually a lot more). The tag is always used unmodified.

    For server environments, the locale identifier has to match the following scheme:

    ll_CC.charset\@modifier

    • ll is the two- or three-letter language code.
    • CC is the optional two-letter country code.
    • charset is an optional character set (letters, digits, and the hyphen).
    • modifier is an optional variant (letters and digits).

    The language code is always converted to lowercase, the country code is converted to uppercase, variant and charset are used as is.

    Returns string

    the locale in use

  • Change the locale.

    For the web you can use all valid language identifier tags that BCP47 allows (and actually a lot more). The tag is always used unmodified.

    For server environments, the locale identifier has to match the following scheme:

    ll_CC.charset\@modifier

    • ll is the two- or three-letter language code.
    • CC is the optional two-letter country code.
    • charset is an optional character set (letters, digits, and the hyphen).
    • modifier is an optional variant (letters and digits).

    The language code is always converted to lowercase, the country code is converted to uppercase, variant and charset are used as is.

    Parameters

    • locale: string

      the locale identifier

    Returns void

    the locale in use

Methods

N_

  • N_(msgid: string): string
  • Does the same as the static method N_().

    Parameters

    • msgid: string

      the message id

    Returns string

    the original string

N_p

  • N_p(_msgctxt: string, msgid: string): string
  • Same as N_() but with context.

    params

    _msgctxt - the message context

    Parameters

    • _msgctxt: string
    • msgid: string

      the message id

    Returns string

    the original string

N_px

  • N_px(_msgctxt: string, msgid: string, placeholders?: Placeholders): string
  • Same as N_() but with context and placeholder expansion.

    Parameters

    • _msgctxt: string
    • msgid: string

      the message id

    • Optional placeholders: Placeholders

      a dictionary of placeholders

    Returns string

    the original string with placeholders expanded

N_x

  • N_x(msgid: string, placeholders?: Placeholders): string
  • Same as N_() but with placeholder expansion.

    Parameters

    • msgid: string

      the message id

    • Optional placeholders: Placeholders

      a dictionary of placeholders

    Returns string

    the original string with placeholders expanded

bindtextdomain

resolve

  • resolve(locale?: string): Promise<Catalog>
  • Resolve a textdomain, i.e. load the LocaleContainer for this domain and all of its dependencies for the currently selected locale or the locale specified.

    The promise will always resolve. If no catalog was found, an empty catalog will be returned that is still usable.

    Parameters

    • Optional locale: string

      an optional locale identifier, defaults to Textdomain.locale

    Returns Promise<Catalog>

    a promise for a Catalog that will always resolve.

textdomain

  • textdomain(): string
  • A textdomain is an identifier for your application or library. It is the basename of your translation files which are either TEXTDOMAIN.json or TEXTDOMAIN.mo, depending on the format you have chosen.

    FIXME! This should be a getter!

    Returns string

    the textdomain

Static N_

  • N_(msgid: string): string
  • A no-op method for string marking.

    Sometimes you want to mark strings for translation but do not actually want to translate them, at least not at the time of their definition. This is often the case, when you have to preserve the original string.

    Take this example:

    orangeColors = [gtx.N_('coral'), gtx.N_('tomato'), gtx.N_('orangered'),
                    gtx.N_('gold'), gtx.N_('orange'), gtx.N_('darkorange')]

    These are standard CSS colors, and you cannot translate them inside CSS styles. But for presentation you may want to translate them later:

    console.log(gtx._x("The css color '{color}' is {translated}.",
                       {
                           color: orangeColors[2],
                           translated: gtx._(orangeColors[2]),
                       }
               )
    );

    In other words: The method just marks strings for translation, so that the extractor esgettext-xgettext finds them but it does not actually translate anything.

    Similar methods are available for other cases (with placeholder expansion, context, or both). They are not available for plural methods because that would not make sense.

    Note that all of these methods are also available as instance methods.

    Parameters

    • msgid: string

      the message id

    Returns string

    the original string

Static N_p

  • N_p(_msgctxt: string, msgid: string): string
  • Does the same as the static method N_p().

    Parameters

    • _msgctxt: string
    • msgid: string

      the message id

    Returns string

    the original string with placeholders expanded

Static N_px

  • N_px(_msgctxt: string, msgid: string, placeholders?: Placeholders): string
  • Does the same as the static method N_px().

    Parameters

    • _msgctxt: string
    • msgid: string

      the message id

    • Optional placeholders: Placeholders

      a dictionary of placeholders

    Returns string

    the original string with placeholders expanded

Static N_x

  • N_x(msgid: string, placeholders?: Placeholders): string
  • Does the same as the static method N_x().

    Parameters

    • msgid: string

      the message id

    • Optional placeholders: Placeholders

      a dictionary of placeholders

    Returns string

    the original string with placeholders expanded

Static clearInstances

  • clearInstances(): void
  • Delete all existing singletons. This method should usually be called only, when you want to free memory.

    Returns void

Static forgetInstances

  • forgetInstances(): void

Static selectLocale

  • selectLocale(supported: Array<string>, requested?: Array<string>): string
  • Select one of the supported locales from a list of locales accepted by the user.

    Parameters

    • supported: Array<string>

      the list of locales supported by the application

    • Optional requested: Array<string>

      the list of locales accepted by the user

      If called with just one argument, then the list of requested locales is determined by calling Textdomain.userLocales.

    Returns string

    the negotiated locale or 'C' if not possible.

Static userLocales

  • userLocales(): Array<string>
  • Queries the user's preferred locales. On the server it queries the environment variables LANGUAGE, LC_ALL, LANG, and LC_MESSAGES (in that order). In the browser, it parses it checks the user preferences in the variables navigator.languages, navigator.language, navigator.userLanguage, navigator.browserLanguage, and navigator.systemLanguage.

    Returns Array<string>

    the set of locales in order of preference

    Added in @runtime 0.1.0.

Generated using TypeDoc