Skip to main content

L10n module

๐Ÿ“– Descriptionโ€‹

The L10n module contains functions for localization. Tripetto uses the GNU gettext system.

๐Ÿ‘ฉโ€๐Ÿ’ป Exampleโ€‹

import { L10n } from "@tripetto/runner";

// Translate message
L10n.gettext("Lorem ipsum dolor sit amet");

// Translate message with shorthand
L10n._("Lorem ipsum dolor sit amet");

// Translate message with arguments
L10n.gettext("Hello %1", "there");
// Shorthand
L10n._("Hello %1", "there");

// Translate plural message
L10n.ngettext("1 user", "%1 users", 2);
// Shorthand
L10n._n("1 user", "%1 users", 2);

๐Ÿ’Ž Classesโ€‹

โ–ถ๏ธ Functionsโ€‹


๐Ÿ”ง _โ€‹

Translates a message (short for gettext).

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global._.

Signatureโ€‹
_(message: string, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
messagestringNoSpecifies the message to translate.
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The first argument is referenced with %1.
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง _nโ€‹

Translates a plural message (short for ngettext).

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global._n.

Signatureโ€‹
_n(message: string, messagePlural: string, count: number, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
messagestringNoSpecifies the message to translate.
messagePluralstringNoSpecifies the plural message to translate.
countnumberNoSpecifies the count for the plural (can be reference in the message with %1).
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The count value is automatically included as the first argument (%1).
Return valueโ€‹

Returns the translated message.

Exampleโ€‹
import { L10n } from "@tripetto/runner";

// Outputs `1 car` to the console.
console.log(L10n._n("1 car", "%1 cars", 1));

// Outputs `2 cars` to the console.
console.log(L10n._n("1 car", "%1 cars", 2));

๐Ÿ”ง dgettextโ€‹

Translates a message using the specified translation domain.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.dgettext.

Signatureโ€‹
dgettext(domain: string, message: string, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
domainstringNoSpecifies the translation domain to use.
messagestringNoSpecifies the message to translate.
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The first argument is referenced with %1.
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง dngettextโ€‹

Translates a plural message using the specified translation domain.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.dngettext.

Signatureโ€‹
dngettext(
domain: string,
message: string,
messagePlural: string,
count: number,
...arguments: string[]
): string
Parametersโ€‹
NameTypeOptionalDescription
domainstringNoSpecifies the translation domain to use.
messagestringNoSpecifies the message to translate.
messagePluralstringNoSpecifies the plural message to translate.
countnumberNoSpecifies the count for the plural (can be reference in the message with %1).
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The count value is automatically included as the first argument (%1).
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง dnpgettextโ€‹

Translates a plural message with the specified context using the specified translation domain.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.dnpgettext.

Signatureโ€‹
dnpgettext(
domain: string,
context: string,
message: string,
messagePlural: string,
count: number,
...arguments: string[]
): string
Parametersโ€‹
NameTypeOptionalDescription
domainstringNoSpecifies the translation domain to use.
contextstringNoSpecifies the translation context.
messagestringNoSpecifies the message to translate.
messagePluralstringNoSpecifies the plural message to translate.
countnumberNoSpecifies the count for the plural (can be reference in the message with %1).
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The count value is automatically included as the first argument (%1).
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง dpgettextโ€‹

Translates a message with the specified context using the specified translation domain.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.dpgettext.

Signatureโ€‹
dpgettext(domain: string, context: string, message: string, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
domainstringNoSpecifies the translation domain to use.
contextstringNoSpecifies the translation context.
messagestringNoSpecifies the message to translate.
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The first argument is referenced with %1.
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง gettextโ€‹

Translates a message.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.gettext.

Signatureโ€‹
gettext(message: string, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
messagestringNoSpecifies the message to translate.
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The first argument is referenced with %1.
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง ngettextโ€‹

Translates a plural message.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.ngettext.

Signatureโ€‹
ngettext(
message: string,
messagePlural: string,
count: number,
...arguments: string[]
): string
Parametersโ€‹
NameTypeOptionalDescription
messagestringNoSpecifies the message to translate.
messagePluralstringNoSpecifies the plural message to translate.
countnumberNoSpecifies the count for the plural (can be reference in the message with %1).
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The count value is automatically included as the first argument (%1).
Return valueโ€‹

Returns the translated message.

Exampleโ€‹
import { L10n } from "@tripetto/runner";

// Outputs `1 car` to the console.
console.log(L10n.ngettext("1 car", "%1 cars", 1));

// Outputs `2 cars` to the console.
console.log(L10n.ngettext("1 car", "%1 cars", 2));

๐Ÿ”ง npgettextโ€‹

Translates a plural message with the specified context.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.npgettext.

Signatureโ€‹
npgettext(
context: string,
message: string,
messagePlural: string,
count: number,
...arguments: string[]
): string
Parametersโ€‹
NameTypeOptionalDescription
contextstringNoSpecifies the translation context.
messagestringNoSpecifies the message to translate.
messagePluralstringNoSpecifies the plural message to translate.
countnumberNoSpecifies the count for the plural (can be reference in the message with %1).
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The count value is automatically included as the first argument (%1).
Return valueโ€‹

Returns the translated message.


๐Ÿ”ง pgettextโ€‹

Translates a message with the specified context.

info

This function uses the global translation namespace (L10n.Namespace.global) and is a shorthand for L10n.Namespace.global.pgettext.

Signatureโ€‹
pgettext(context: string, message: string, ...arguments: string[]): string
Parametersโ€‹
NameTypeOptionalDescription
contextstringNoSpecifies the translation context.
messagestringNoSpecifies the message to translate.
argumentsstring[]YesOptional string arguments which can be referenced in the message using the percent sign followed by the argument index %n. The first argument is referenced with %1.
Return valueโ€‹

Returns the translated message.

โ›“๏ธ Interfacesโ€‹


๐Ÿ”— IDomainโ€‹

Describes the interface for the domain object.

Type declarationโ€‹
interface IDomain {
  locale: string;Readonly
  language: string;Readonly
  native: string;Readonly
}
๐Ÿ–ฑ๏ธ Hover with the mouse over a property name for a tooltip with the description of that property. Click it for more information.

๐Ÿท๏ธ localeโ€‹

Specifies the locale (language[_territory]).

Typeโ€‹

string


๐Ÿท๏ธ languageโ€‹

Specifies the language in english.

Typeโ€‹

string


๐Ÿท๏ธ nativeโ€‹

Specifies the language in the native language.

Typeโ€‹

string


๐Ÿ”— ILocaleโ€‹

Describes the interface for the locale object. Locale information is stored in a JSON file per locale and are stored in the runner/locales folder of each stock runner package.

Type declarationโ€‹
interface ILocale {
locale: string;
domain: string;
direction: "ltr" | "rtl";
countryCode: string;
country: string;
countryNative: string;
language: string;
languageNative: string;
translations: {
months: {
formatted: {
abbreviated: string[];
narrow: string[];
wide: string[];
};
nominative: {
abbreviated: string[];
narrow: string[];
wide: string[];
};
};
days: {
formatted: {
abbreviated: string[];
narrow: string[];
short: string[];
wide: string[];
};
nominative: {
abbreviated: string[];
narrow: string[];
short: string[];
wide: string[];
};
};
time: {
AM: string;
PM: string;
};
};
formats: {
date: {
full: string;
long: string;
medium: string;
short: string;
};
time: {
full: string;
long: string;
medium: string;
short: string;
};
dateTime: {
full: string;
long: string;
medium: string;
short: string;
};
numbers: {
decimals: string;
grouping: string;
minus: string;
};
};
}

๐Ÿ“œ Typesโ€‹


๐Ÿ“ƒ TTranslationโ€‹

Defines a translation.

Typeโ€‹
{
"": {
language?: string;
"plural-forms"?: string;
"plural-family"?: string;
};
} & {
[id: string]: [null | string, ...string[]] | [null | string, [string]];
}