delay i18n loads

This commit is contained in:
Kyle Spearrin 2018-01-12 21:51:07 -05:00
parent 76752f81c6
commit c0262306f8
2 changed files with 29 additions and 27 deletions

View File

@ -109,11 +109,12 @@ export default class MainBackground {
// Services
this.utilsService = new UtilsService();
this.platformUtilsService = new BrowserPlatformUtilsService();
const delayi18nLoad = this.platformUtilsService.isEdge() || this.platformUtilsService.isSafari() ? 1000 : 0;
this.messagingService = new BrowserMessagingService(this.platformUtilsService);
this.storageService = new BrowserStorageService(this.platformUtilsService, false);
this.secureStorageService = new BrowserStorageService(this.platformUtilsService, true);
this.i18nService = i18nService(this.platformUtilsService);
this.constantsService = new ConstantsService(this.i18nService, this.platformUtilsService);
this.constantsService = new ConstantsService(this.i18nService, delayi18nLoad);
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService);
this.tokenService = new TokenService(this.storageService);
this.appIdService = new AppIdService(this.storageService);
@ -124,8 +125,8 @@ export default class MainBackground {
this.settingsService = new SettingsService(this.userService, this.storageService);
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService,
this.apiService, this.storageService);
this.folderService = new FolderService(this.cryptoService, this.userService, this.i18nService.noneFolder,
this.apiService, this.storageService);
this.folderService = new FolderService(this.cryptoService, this.userService,
() => this.i18nService.noneFolder, delayi18nLoad, this.apiService, this.storageService);
this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService);
this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService,
this.cryptoService, this.platformUtilsService, this.storageService,

View File

@ -1,28 +1,16 @@
import { PlatformUtilsService } from 'jslib/abstractions';
// 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',
];
export default function i18nService(platformUtilsService: PlatformUtilsService) {
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(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);
}
}
}
if (platformUtilsService.isEdge()) {
loadMessages('../_locales/', 'en', localeMessages,
(prop: string, message: string) => chrome.i18n.getMessage(prop));
@ -31,18 +19,19 @@ export default function i18nService(platformUtilsService: PlatformUtilsService)
if (platformUtilsService.isSafari()) {
let lang = navigator.language;
if (supportedLocales.indexOf(lang) === -1) {
if (SupportedLocales.indexOf(lang) === -1) {
lang = lang.slice(0, 2);
if (supportedLocales.indexOf(lang) === -1) {
lang = supportedLocales[0];
if (SupportedLocales.indexOf(lang) === -1) {
lang = SupportedLocales[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 (lang !== SupportedLocales[0]) {
loadMessages(dir, SupportedLocales[0], defaultMessages,
(prop: string, message: string) => message);
}
}
@ -66,3 +55,15 @@ export default function i18nService(platformUtilsService: PlatformUtilsService)
},
});
}
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);
}
}
}