i18n service for safari

This commit is contained in:
Kyle Spearrin 2018-01-11 22:13:57 -05:00
parent 6352fb187e
commit b2f2cff117
1 changed files with 50 additions and 12 deletions

View File

@ -1,25 +1,63 @@
import { PlatformUtilsService } from 'jslib/abstractions';
export default function i18nService(platformUtilsService: PlatformUtilsService) {
const edgeMessages: any = {};
const defaultMessages: any = {};
const localeMessages: any = {};
// First locale is the default (English)
const supportedLocales = [
'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',
];
async function loadMessages(locale: string, messagesObj: any,
messageCallback: (prop: string, message: string) => string): Promise<any> {
const formattedLocale = locale.replace('-', '_');
const file = await fetch('../_locales/' + formattedLocale + '/messages.json');
const locales = await file.json();
for (const prop in locales) {
if (locales.hasOwnProperty(prop)) {
messagesObj[prop] = messageCallback(prop, locales[prop].message);
}
}
}
if (platformUtilsService.isEdge()) {
fetch('../_locales/en/messages.json').then((file) => {
return file.json();
}).then((locales) => {
for (const prop in locales) {
if (locales.hasOwnProperty(prop)) {
edgeMessages[prop] = chrome.i18n.getMessage(prop);
}
}
});
loadMessages('en', localeMessages, (prop: string, message: string) => chrome.i18n.getMessage(prop));
return localeMessages;
}
return edgeMessages;
if (platformUtilsService.isSafari()) {
let lang = navigator.language;
if (supportedLocales.indexOf(lang) === -1) {
lang = lang.slice(0, 2);
if (supportedLocales.indexOf(lang) === -1) {
lang = supportedLocales[0];
}
}
loadMessages(lang, localeMessages, (prop: string, message: string) => message);
if (lang !== supportedLocales[0]) {
loadMessages(supportedLocales[0], defaultMessages, (prop: string, message: string) => message);
}
}
return new Proxy({}, {
get: (target, name) => {
return chrome.i18n.getMessage(name);
const id = name.toString();
if (platformUtilsService.isSafari()) {
if (localeMessages.hasOwnProperty(id) && localeMessages[id]) {
return localeMessages[id];
} else if (defaultMessages.hasOwnProperty(id) && defaultMessages[id]) {
return defaultMessages[id];
}
return '';
}
return chrome.i18n.getMessage(id);
},
set: (target, name, value) => {
return false;