mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 19:23:52 +00:00
converted i18nservice properly
This commit is contained in:
@@ -1,69 +1,121 @@
|
||||
import { PlatformUtilsService } from 'jslib/abstractions';
|
||||
import { I18nService as I18nServiceAbstraction } from 'jslib/abstractions/i18n.service';
|
||||
|
||||
// First locale is the default (English)
|
||||
const SupportedLocales = [
|
||||
const SupportedTranslationLocales = [
|
||||
'en', 'cs', 'da', 'de', 'es', 'et', 'fi', 'fr', 'hr', 'hu', 'id', 'it', 'ja',
|
||||
'nb', 'nl', 'pl', 'pt-BR', 'pt-PT', 'ro', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi',
|
||||
'zh-CN', 'zh-TW',
|
||||
];
|
||||
|
||||
export default function i18nService(platformUtilsService: PlatformUtilsService) {
|
||||
const defaultMessages: any = {};
|
||||
const localeMessages: any = {};
|
||||
export default class I18nService implements I18nServiceAbstraction {
|
||||
defaultMessages: any = {};
|
||||
localeMessages: any = {};
|
||||
locale: string;
|
||||
translationLocale: string;
|
||||
collator: Intl.Collator;
|
||||
inited: boolean;
|
||||
|
||||
if (platformUtilsService.isEdge()) {
|
||||
loadMessages('../_locales/', 'en', localeMessages,
|
||||
(prop: string, message: string) => chrome.i18n.getMessage(prop));
|
||||
return localeMessages;
|
||||
}
|
||||
constructor(private systemLanguage: string, private localesDirectory: string) { }
|
||||
|
||||
if (platformUtilsService.isSafari()) {
|
||||
let lang = navigator.language;
|
||||
if (SupportedLocales.indexOf(lang) === -1) {
|
||||
lang = lang.slice(0, 2);
|
||||
async init(locale?: string) {
|
||||
if (this.inited) {
|
||||
throw new Error('i18n already initialized.');
|
||||
}
|
||||
|
||||
if (SupportedLocales.indexOf(lang) === -1) {
|
||||
lang = SupportedLocales[0];
|
||||
this.inited = true;
|
||||
this.locale = this.translationLocale = locale != null ? locale : this.systemLanguage;
|
||||
this.collator = new Intl.Collator(this.locale);
|
||||
|
||||
if (SupportedTranslationLocales.indexOf(this.translationLocale) === -1) {
|
||||
this.translationLocale = this.translationLocale.slice(0, 2);
|
||||
|
||||
if (SupportedTranslationLocales.indexOf(this.translationLocale) === -1) {
|
||||
this.translationLocale = SupportedTranslationLocales[0];
|
||||
}
|
||||
}
|
||||
|
||||
const dir = './_locales/';
|
||||
loadMessages(dir, lang, localeMessages, (prop: string, message: string) => message);
|
||||
if (lang !== SupportedLocales[0]) {
|
||||
loadMessages(dir, SupportedLocales[0], defaultMessages,
|
||||
(prop: string, message: string) => message);
|
||||
if (this.localesDirectory != null) {
|
||||
await this.loadMessages(this.localesDirectory, this.translationLocale, this.localeMessages);
|
||||
if (this.translationLocale !== SupportedTranslationLocales[0]) {
|
||||
await this.loadMessages(this.localesDirectory, SupportedTranslationLocales[0], this.defaultMessages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Proxy({}, {
|
||||
get: (target, name) => {
|
||||
const id = name.toString();
|
||||
t(id: string, p1?: string, p2?: string, p3?: string): string {
|
||||
return this.translate(id, p1, p2, p3);
|
||||
}
|
||||
|
||||
if (platformUtilsService.isSafari()) {
|
||||
if (localeMessages.hasOwnProperty(id) && localeMessages[id]) {
|
||||
return localeMessages[id];
|
||||
} else if (defaultMessages.hasOwnProperty(id) && defaultMessages[id]) {
|
||||
return defaultMessages[id];
|
||||
translate(id: string, p1?: string, p2?: string, p3?: string): string {
|
||||
if (this.localesDirectory == null) {
|
||||
const placeholders: string[] = [];
|
||||
if (p1 != null) {
|
||||
placeholders.push(p1);
|
||||
}
|
||||
if (p2 != null) {
|
||||
placeholders.push(p2);
|
||||
}
|
||||
if (p3 != null) {
|
||||
placeholders.push(p3);
|
||||
}
|
||||
|
||||
if (placeholders.length) {
|
||||
return chrome.i18n.getMessage(id, placeholders);
|
||||
} else {
|
||||
return chrome.i18n.getMessage(id);
|
||||
}
|
||||
}
|
||||
|
||||
let result: string;
|
||||
if (this.localeMessages.hasOwnProperty(id) && this.localeMessages[id]) {
|
||||
result = this.localeMessages[id];
|
||||
} else if (this.defaultMessages.hasOwnProperty(id) && this.defaultMessages[id]) {
|
||||
result = this.defaultMessages[id];
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
private async loadMessages(localesDir: string, locale: string, messagesObj: any): Promise<any> {
|
||||
const formattedLocale = locale.replace('-', '_');
|
||||
const file = await fetch(localesDir + formattedLocale + '/messages.json');
|
||||
const locales = await file.json();
|
||||
for (const prop in locales) {
|
||||
if (!locales.hasOwnProperty(prop)) {
|
||||
continue;
|
||||
}
|
||||
messagesObj[prop] = locales[prop].message;
|
||||
|
||||
if (locales[prop].placeholders) {
|
||||
for (const placeProp in locales[prop].placeholders) {
|
||||
if (!locales[prop].placeholders.hasOwnProperty(placeProp) ||
|
||||
!locales[prop].placeholders[placeProp].content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const replaceToken = '\\$' + placeProp.toUpperCase() + '\\$';
|
||||
let replaceContent = locales[prop].placeholders[placeProp].content;
|
||||
if (replaceContent === '$1' || replaceContent === '$2' || replaceContent === '$3') {
|
||||
replaceContent = '__' + replaceContent + '__';
|
||||
}
|
||||
messagesObj[prop] = messagesObj[prop].replace(new RegExp(replaceToken, 'g'), replaceContent);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
return chrome.i18n.getMessage(id);
|
||||
},
|
||||
set: (target, name, value) => {
|
||||
return false;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadMessages(localesDir: string, locale: string, messagesObj: any,
|
||||
messageCallback: (prop: string, message: string) => string): Promise<any> {
|
||||
const formattedLocale = locale.replace('-', '_');
|
||||
const file = await fetch(localesDir + formattedLocale + '/messages.json');
|
||||
const locales = await file.json();
|
||||
for (const prop in locales) {
|
||||
if (locales.hasOwnProperty(prop)) {
|
||||
messagesObj[prop] = messageCallback(prop, locales[prop].message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import { I18nService as I18nServiceAbstraction } from 'jslib/abstractions/i18n.service';
|
||||
|
||||
export default class I18n2Service implements I18nServiceAbstraction {
|
||||
locale: string;
|
||||
translationLocale: string;
|
||||
collator: Intl.Collator;
|
||||
inited: boolean;
|
||||
|
||||
constructor(private systemLanguage: string, private i18nService: any) {
|
||||
}
|
||||
|
||||
async init(locale?: string) {
|
||||
if (this.inited) {
|
||||
throw new Error('i18n already initialized.');
|
||||
}
|
||||
|
||||
this.inited = true;
|
||||
this.locale = this.translationLocale = locale != null ? locale : this.systemLanguage;
|
||||
this.collator = new Intl.Collator(this.locale);
|
||||
}
|
||||
|
||||
t(id: string, p1?: string, p2?: string, p3?: string): string {
|
||||
return this.translate(id);
|
||||
}
|
||||
|
||||
translate(id: string, p1?: string, p2?: string, p3?: string): string {
|
||||
return this.i18nService[id];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user