From c0262306f8f9724985f46165b9148c1fbf57f050 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 12 Jan 2018 21:51:07 -0500 Subject: [PATCH] delay i18n loads --- src/background/main.background.ts | 7 +++-- src/services/i18n.service.ts | 49 ++++++++++++++++--------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 301430648d..f121e8ee9b 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -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, diff --git a/src/services/i18n.service.ts b/src/services/i18n.service.ts index 32aa3b8342..b27ffb76b8 100644 --- a/src/services/i18n.service.ts +++ b/src/services/i18n.service.ts @@ -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 { - 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 { + 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); + } + } +}