From f382f125be0f7ce76098c303d13490bff500753e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 26 Jan 2018 22:38:54 -0500 Subject: [PATCH] i18n2service --- src/background/main.background.ts | 11 ++++++++--- src/services/i18n2.service.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/services/i18n2.service.ts diff --git a/src/background/main.background.ts b/src/background/main.background.ts index a6fb9f0439..2c1fd75d86 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -28,6 +28,7 @@ import { CryptoService as CryptoServiceAbstraction, EnvironmentService as EnvironmentServiceAbstraction, FolderService as FolderServiceAbstraction, + I18nService as I18nServiceAbstraction, LockService as LockServiceAbstraction, MessagingService as MessagingServiceAbstraction, PasswordGenerationService as PasswordGenerationServiceAbstraction, @@ -58,6 +59,7 @@ import BrowserMessagingService from '../services/browserMessaging.service'; import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service'; import BrowserStorageService from '../services/browserStorage.service'; import i18nService from '../services/i18n.service'; +import I18n2Service from '../services/i18n2.service'; import { AutofillService as AutofillServiceAbstraction } from '../services/abstractions/autofill.service'; @@ -66,6 +68,7 @@ export default class MainBackground { storageService: StorageServiceAbstraction; secureStorageService: StorageServiceAbstraction; i18nService: any; + i18n2Service: I18nServiceAbstraction; platformUtilsService: PlatformUtilsServiceAbstraction; utilsService: UtilsServiceAbstraction; constantsService: ConstantsService; @@ -115,6 +118,7 @@ export default class MainBackground { this.storageService = new BrowserStorageService(this.platformUtilsService, false); this.secureStorageService = new BrowserStorageService(this.platformUtilsService, true); this.i18nService = i18nService(this.platformUtilsService); + this.i18n2Service = new I18n2Service(window.navigator.language, i18nService); this.constantsService = new ConstantsService(this.i18nService, delayi18nLoad); this.cryptoService = new CryptoService(this.storageService, this.secureStorageService); this.tokenService = new TokenService(this.storageService); @@ -125,10 +129,11 @@ export default class MainBackground { this.userService = new UserService(this.tokenService, this.storageService); this.settingsService = new SettingsService(this.userService, this.storageService); this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, - this.apiService, this.storageService); + this.apiService, this.storageService, this.i18n2Service); this.folderService = new FolderService(this.cryptoService, this.userService, - () => this.i18nService.noneFolder, this.apiService, this.storageService); - this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService); + () => this.i18nService.noneFolder, this.apiService, this.storageService, this.i18n2Service); + this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, + this.i18n2Service); this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService, this.cryptoService, this.platformUtilsService, this.storageService, () => this.setIcon(), () => this.refreshBadgeAndMenu()); diff --git a/src/services/i18n2.service.ts b/src/services/i18n2.service.ts new file mode 100644 index 0000000000..b8b7ad4d1e --- /dev/null +++ b/src/services/i18n2.service.ts @@ -0,0 +1,29 @@ +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): string { + return this.translate(id); + } + + translate(id: string): string { + return this.i18nService[id]; + } +}