bitwarden-estensione-browser/libs/common/src/services/i18n.service.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

176 lines
5.5 KiB
TypeScript
Raw Normal View History

2018-04-11 21:07:33 +02:00
import { I18nService as I18nServiceAbstraction } from "../abstractions/i18n.service";
export class I18nService implements I18nServiceAbstraction {
locale: string;
// First locale is the default (English)
supportedTranslationLocales: string[] = ["en"];
2018-04-11 21:07:33 +02:00
translationLocale: string;
collator: Intl.Collator;
2019-09-06 15:33:09 +02:00
localeNames = new Map<string, string>([
["af", "Afrikaans"],
["az", "Azərbaycanca"],
["be", "Беларуская"],
2019-09-06 15:33:09 +02:00
["bg", "български"],
2022-02-25 18:54:27 +01:00
["bn", "বাংলা"],
["bs", "bosanski jezik"],
2019-09-06 15:33:09 +02:00
["ca", "català"],
["cs", "čeština"],
["da", "dansk"],
["de", "Deutsch"],
["el", "Ελληνικά"],
["en", "English"],
["en-GB", "English (British)"],
2022-02-25 18:54:27 +01:00
["en-IN", "English (India)"],
2019-09-06 15:33:09 +02:00
["eo", "Esperanto"],
["es", "español"],
["et", "eesti"],
["fa", "فارسی"],
["fi", "suomi"],
2022-02-25 18:54:27 +01:00
["fil", "Wikang Filipino"],
2019-09-06 15:33:09 +02:00
["fr", "français"],
["he", "עברית"],
["hi", "हिन्दी"],
["hr", "hrvatski"],
["hu", "magyar"],
["id", "Bahasa Indonesia"],
["it", "italiano"],
["ja", "日本語"],
2022-02-25 18:54:27 +01:00
["ka", "ქართული"],
["km", "ខ្មែរ, ខេមរភាសា, ភាសាខ្មែរ"],
["kn", "ಕನ್ನಡ"],
2019-09-06 15:33:09 +02:00
["ko", "한국어"],
2022-02-25 18:54:27 +01:00
["lt", "lietuvių kalba"],
["lv", "Latvietis"],
2022-02-25 18:54:27 +01:00
["me", "црногорски"],
["ml", "മലയാളം"],
2019-09-06 15:33:09 +02:00
["nb", "norsk (bokmål)"],
["nl", "Nederlands"],
2022-02-25 18:54:27 +01:00
["nn", "Norsk Nynorsk"],
2019-09-06 15:33:09 +02:00
["pl", "polski"],
["pt-BR", "português do Brasil"],
["pt-PT", "português"],
["ro", "română"],
["ru", "русский"],
2022-02-25 18:54:27 +01:00
["si", "සිංහල"],
2019-09-06 15:33:09 +02:00
["sk", "slovenčina"],
2022-02-25 18:54:27 +01:00
["sl", "Slovenski jezik, Slovenščina"],
2020-11-18 21:58:12 +01:00
["sr", "Српски"],
2019-09-06 15:33:09 +02:00
["sv", "svenska"],
["th", "ไทย"],
["tr", "Türkçe"],
["uk", "українська"],
["vi", "Tiếng Việt"],
["zh-CN", "中文(中国大陆)"],
["zh-TW", "中文(台灣)"],
]);
2021-12-16 13:36:21 +01:00
2018-04-11 21:07:33 +02:00
protected inited: boolean;
protected defaultMessages: any = {};
protected localeMessages: any = {};
2021-12-16 13:36:21 +01:00
constructor(
protected systemLanguage: string,
protected localesDirectory: string,
2018-04-15 02:17:10 +02:00
protected getLocalesJson: (formattedLocale: string) => Promise<any>
) {
this.systemLanguage = systemLanguage.replace("_", "-");
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
async init(locale?: string) {
if (this.inited) {
throw new Error("i18n already initialized.");
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
if (this.supportedTranslationLocales == null || this.supportedTranslationLocales.length === 0) {
throw new Error("supportedTranslationLocales not set.");
2018-04-15 02:17:10 +02:00
}
2018-04-11 21:07:33 +02:00
this.inited = true;
this.locale = this.translationLocale = locale != null ? locale : this.systemLanguage;
try {
this.collator = new Intl.Collator(this.locale, { numeric: true, sensitivity: "base" });
} catch {
this.collator = null;
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
if (this.supportedTranslationLocales.indexOf(this.translationLocale) === -1) {
this.translationLocale = this.translationLocale.slice(0, 2);
if (this.supportedTranslationLocales.indexOf(this.translationLocale) === -1) {
this.translationLocale = this.supportedTranslationLocales[0];
2018-04-11 21:07:33 +02:00
}
}
if (this.localesDirectory != null) {
await this.loadMessages(this.translationLocale, this.localeMessages);
2018-04-11 21:07:33 +02:00
if (this.translationLocale !== this.supportedTranslationLocales[0]) {
await this.loadMessages(this.supportedTranslationLocales[0], this.defaultMessages);
2021-12-16 13:36:21 +01:00
}
}
}
2018-04-11 21:07:33 +02:00
t(id: string, p1?: string, p2?: string, p3?: string): string {
return this.translate(id, p1, p2, p3);
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
translate(id: string, p1?: string, p2?: string, p3?: string): string {
let result: string;
2022-02-22 15:39:11 +01:00
// eslint-disable-next-line
2018-04-11 21:07:33 +02:00
if (this.localeMessages.hasOwnProperty(id) && this.localeMessages[id]) {
result = this.localeMessages[id];
2022-02-22 15:39:11 +01:00
// eslint-disable-next-line
2018-04-11 21:07:33 +02:00
} else if (this.defaultMessages.hasOwnProperty(id) && this.defaultMessages[id]) {
result = this.defaultMessages[id];
2021-12-16 13:36:21 +01:00
} else {
2018-04-11 21:07:33 +02:00
result = "";
}
if (result !== "") {
if (p1 != null) {
result = result.split("__$1__").join(p1);
}
if (p2 != null) {
result = result.split("__$2__").join(p2);
}
if (p3 != null) {
result = result.split("__$3__").join(p3);
}
}
return result;
2021-12-16 13:36:21 +01:00
}
private async loadMessages(locale: string, messagesObj: any): Promise<any> {
2018-04-11 21:07:33 +02:00
const formattedLocale = locale.replace("-", "_");
const locales = await this.getLocalesJson(formattedLocale);
2018-04-11 21:07:33 +02:00
for (const prop in locales) {
2022-02-22 15:39:11 +01:00
// eslint-disable-next-line
2018-04-11 21:07:33 +02:00
if (!locales.hasOwnProperty(prop)) {
continue;
}
messagesObj[prop] = locales[prop].message;
2021-12-16 13:36:21 +01:00
2018-04-11 21:07:33 +02:00
if (locales[prop].placeholders) {
for (const placeProp in locales[prop].placeholders) {
if (
2022-02-22 15:39:11 +01:00
!locales[prop].placeholders.hasOwnProperty(placeProp) || // eslint-disable-line
2018-04-11 21:07:33 +02:00
!locales[prop].placeholders[placeProp].content
) {
continue;
}
2021-12-16 13:36:21 +01:00
2018-04-11 21:07:33 +02:00
const replaceToken = "\\$" + placeProp.toUpperCase() + "\\$";
let replaceContent = locales[prop].placeholders[placeProp].content;
if (replaceContent === "$1" || replaceContent === "$2" || replaceContent === "$3") {
2019-01-27 03:30:53 +01:00
replaceContent = "__$" + replaceContent + "__";
2018-04-11 21:07:33 +02:00
}
messagesObj[prop] = messagesObj[prop].replace(
new RegExp(replaceToken, "g"),
replaceContent
);
}
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
}
2021-12-16 13:36:21 +01:00
}
2018-04-11 21:07:33 +02:00
}