load locale files by require

This commit is contained in:
Kyle Spearrin 2018-01-24 15:22:13 -05:00
parent 2ab23c2c9e
commit e42bf66b2f
3 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,8 @@
{ {
"hello": { "hello": {
"message": "hello" "message": "hello"
},
"world": {
"message": "world"
} }
} }

View File

@ -2,7 +2,7 @@ import { webFrame } from 'electron';
import { import {
APP_INITIALIZER, APP_INITIALIZER,
NgModule NgModule,
} from '@angular/core'; } from '@angular/core';
import { DesktopMessagingService } from '../../services/desktopMessaging.service'; import { DesktopMessagingService } from '../../services/desktopMessaging.service';
@ -112,8 +112,8 @@ function initFactory(i18n: I18nService): Function {
provide: APP_INITIALIZER, provide: APP_INITIALIZER,
useFactory: initFactory, useFactory: initFactory,
deps: [I18nService], deps: [I18nService],
multi: true multi: true,
} },
], ],
}) })
export class ServicesModule { export class ServicesModule {

View File

@ -1,3 +1,5 @@
import * as path from 'path';
// First locale is the default (English) // First locale is the default (English)
const SupportedLocales = [ const SupportedLocales = [
'en', 'es', 'en', 'es',
@ -49,14 +51,16 @@ export class I18nService {
return ''; return '';
} }
private async loadMessages(locale: string, messagesObj: any): Promise<any> { private loadMessages(locale: string, messagesObj: any): Promise<any> {
const formattedLocale = locale.replace('-', '_'); const formattedLocale = locale.replace('-', '_');
const file = await fetch(this.localesDirectory + '/' + formattedLocale + '/messages.json'); const filePath = path.join(__dirname, this.localesDirectory + '/' + formattedLocale + '/messages.json');
const locales = await file.json(); const locales = (window as any).require(filePath);
for (const prop in locales) { for (const prop in locales) {
if (locales.hasOwnProperty(prop)) { if (locales.hasOwnProperty(prop)) {
messagesObj[prop] = locales[prop].message; messagesObj[prop] = locales[prop].message;
} }
} }
return Promise.resolve();
} }
} }