This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppModule.ts
170 lines (151 loc) · 4.52 KB
/
AppModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { APP_INITIALIZER, ApplicationRef, LOCALE_ID, NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { createInputTransfer, createNewHosts, removeNgStyles } from '@angularclass/hmr';
import { I18NEXT_SERVICE, I18NextLoadResult, I18NextModule, ITranslationService, defaultInterpolationFormat } from 'angular-i18next';
import { I18NextValidationMessageModule } from 'angular-validation-message';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { AppComponent } from './AppComponent';
import { AccessDeniedComponent } from './content/access-denied/access-denied.component';
import { SimpleDemoComponent } from './content/simple-demo.component';
import { ENV_PROVIDERS } from './environment';
import { AppRouterModule } from './routing/AppRouterModule';
import { AppErrorComponent } from './structure/app-error.component';
import { AppFooterComponent } from './structure/app-footer.component';
import { AppHeaderComponent } from './structure/app-header.component';
import { HeaderLanguageComponent } from './structure/header-controls/header.language.component';
/*
* Platform and Environment providers/directives/pipes
*/
const i18nextOptions = {
whitelist: ['en', 'ru'],
fallbackLng: 'en',
debug: true, // set debug?
returnEmptyString: false,
ns: [
'translation',
'validation',
'error',
// 'feature.rich_form'
],
interpolation: {
format: I18NextModule.interpolationFormat(defaultInterpolationFormat)
},
//backend plugin options
backend: {
loadPath: 'locales/{{lng}}.{{ns}}.json'
},
// lang detection plugin options
detection: {
// order and from where user language should be detected
order: ['cookie'],
// keys or params to lookup language from
lookupCookie: 'lang',
// cache user language on
caches: ['cookie'],
// optional expire and domain for set cookie
cookieMinutes: 10080, // 7 days
cookieDomain: I18NEXT_LANG_COOKIE_DOMAIN
}
};
export function appInit(i18next: ITranslationService) {
return () => {
let promise: Promise<I18NextLoadResult> = i18next
.use(HttpApi)
.use<any>(LanguageDetector)
.init(i18nextOptions);
return promise;
};
}
export function localeIdFactory(i18next: ITranslationService) {
return i18next.language;
}
export const I18N_PROVIDERS = [
{
provide: APP_INITIALIZER,
useFactory: appInit,
deps: [I18NEXT_SERVICE],
multi: true
},
{
provide: LOCALE_ID,
deps: [I18NEXT_SERVICE],
useFactory: localeIdFactory
},
];
type StoreType = {
//state: InternalStateType,
restoreInputValues: () => void,
disposeOldHosts: () => void
};
/**
* `AppModule` is the main entry point into Angular2's bootstraping process
*/
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
//app
AppComponent,
AppHeaderComponent,
AppFooterComponent,
HeaderLanguageComponent,
AppErrorComponent,
SimpleDemoComponent,
AccessDeniedComponent
],
imports: [ // import Angular's modules
//core
BrowserModule,
FormsModule,
//lib
I18NextModule.forRoot({
// errorHandlingStrategy: StrictErrorHandlingStrategy
}),
//app
AppRouterModule,
I18NextValidationMessageModule
],
exports: [
],
providers: [ // expose our Services and Providers into Angular's dependency injection
ENV_PROVIDERS,
I18N_PROVIDERS
],
entryComponents: [
]
})
export class AppModule {
constructor(public appRef: ApplicationRef) {}
hmrOnInit(store: StoreType) {
if (!store) return;
console.log('HMR store', JSON.stringify(store, null, 2));
// set state
//this.appState._state = store.state;
// set input values
if ('restoreInputValues' in store) {
let restoreInputValues = store.restoreInputValues;
setTimeout(restoreInputValues);
}
this.appRef.tick();
//delete store.state;
delete store.restoreInputValues;
}
hmrOnDestroy(store: StoreType) {
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
// save state
//const state = this.appState._state;
//store.state = state;
// recreate root elements
store.disposeOldHosts = createNewHosts(cmpLocation);
// save input values
store.restoreInputValues = createInputTransfer();
// remove styles
removeNgStyles();
}
hmrAfterDestroy(store: StoreType) {
// display new elements
store.disposeOldHosts();
delete store.disposeOldHosts;
}
}