This NPM package created for simple and flexible localization of your applications.
npm install dk-i18n # you can use yarn or pnpm instead
import { I18N } from 'simple-i18n';
const i18n = new I18N({
initLocale: "ua",
fallbackLocale: "en"
});
i18n.addLocale("en", {
hello: "Hello, {name}!",
names: ["John", "Jane", "Joe"]
});
i18n.addLocale("ua", {
hello: "Привіт, {name}!",
names: ["Іван", "Іванна", "Іванко"]
});
console.log(i18n.t("hello", {}, { name: "John" })); // Привіт, John!
console.log(i18n.t("names.0")); // Іван
console.log(i18n.t("names"), { json: true }); // you also can get all translations as JSON
Creates new instance of I18N class.
class I18N {
options: I18NOptions;
locales: Map<string, Locale>;
currentLocale: string;
constructor(options: I18NOptions);
useLocale(locale: string): void;
addLocale(name: string, locale: ILocale): void;
t(key: string, options?: TranslationOptions, values?: TranslationValues): string | string[] | ILocale | undefined;
}
Creates new instance of Locale class.
class Locale {
private locale;
constructor(locale: ILocale);
t(key: string, options?: TranslationOptions, values?: TranslationValues): string | string[] | ILocale | undefined;
}
interface I18NOptions {
initLocale?: string;
fallbackLocale?: string;
}
interface ILocale {
[key: string]: string | string[] | ILocale;
}
interface TranslationOptions {
json?: boolean;
}
interface TranslationValues {
[key: string]: string | number | boolean;
}