-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load translations with webpack
- Loading branch information
Showing
5 changed files
with
56 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
import { registerLocaleData } from '@angular/common'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import localeDe from '@angular/common/locales/de'; | ||
import localeFr from '@angular/common/locales/fr'; | ||
import { Inject, LOCALE_ID, NgModule } from '@angular/core'; | ||
import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core'; | ||
import { TranslateHttpLoader } from '@ngx-translate/http-loader'; | ||
import { Observable, from } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
export function translateFactory(http: HttpClient) { | ||
return new TranslateHttpLoader(http, 'assets/i18n/', '.json'); | ||
class WebpackTranslateLoader implements TranslateLoader { | ||
getTranslation(lang: string): Observable<unknown> { | ||
return from(import(`../../assets/i18n/${lang}.json`)).pipe( | ||
tap(() => { | ||
switch (lang) { | ||
case 'de_DE': | ||
import('@angular/common/locales/global/de'); | ||
break; | ||
case 'fr_FR': | ||
import('@angular/common/locales/global/fr'); | ||
break; | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [ | ||
TranslateModule.forRoot({ | ||
loader: { | ||
provide: TranslateLoader, | ||
useFactory: translateFactory, | ||
deps: [HttpClient], | ||
useClass: WebpackTranslateLoader, | ||
}, | ||
}), | ||
], | ||
}) | ||
export class InternationalizationModule { | ||
constructor(@Inject(LOCALE_ID) lang: string, translateService: TranslateService) { | ||
registerLocaleData(localeDe); | ||
registerLocaleData(localeFr); | ||
|
||
translateService.setDefaultLang(lang.replace(/\-/, '_')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8c725de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Azure Demo Servers are available: