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]];
}