Skip to content

Commit

Permalink
refactor: build back new translation loading approach
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed May 21, 2021
1 parent 4b07610 commit aab383f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 33 deletions.
12 changes: 9 additions & 3 deletions docs/concepts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,16 @@ To add other languages except English, German or French:
{ lang: 'nl_NL', currency: 'EUR', value: 'nl', displayName: 'Dutch', displayLong: 'Dutch (Netherlands)' }
```
3. Import a [global variant of the locale data](https://angular.io/guide/i18n#import-global-variants-of-the-locale-data) in the [`InternationalizationModule`](../../src/app/core/internationalization.module.ts), e.g.
3. Import the Angular locale data in the [`InternationalizationModule`](../../src/app/core/internationalization.module.ts):
```typescript
import localeNl from '@angular/common/locales/nl';
```
4. Register the locale using `registerLocaleData` in the constructor:
```typescript
case 'nl_NL':
return import('@angular/common/locales/global/nl');
registerLocaleData(localeNl);
```
# Further References
Expand Down
24 changes: 8 additions & 16 deletions src/app/core/internationalization.module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { registerLocaleData } from '@angular/common';
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 { Observable, from, of } from 'rxjs';
import { map, switchMap, switchMapTo } from 'rxjs/operators';
import { Observable, from } from 'rxjs';

class WebpackTranslateLoader implements TranslateLoader {
getTranslation(language: string): Observable<unknown> {
return of(language).pipe(
map(lang => {
switch (lang) {
case 'de_DE':
return import('@angular/common/locales/global/de');
case 'fr_FR':
return import('@angular/common/locales/global/fr');
}
}),
switchMap(imp => {
const translations$ = from(import(`../../assets/i18n/${language}.json`));
return imp ? from(imp).pipe(switchMapTo(translations$)) : translations$;
})
);
return from(import(`../../assets/i18n/${language}.json`));
}
}

Expand All @@ -34,6 +23,9 @@ class WebpackTranslateLoader implements TranslateLoader {
})
export class InternationalizationModule {
constructor(@Inject(LOCALE_ID) lang: string, translateService: TranslateService) {
registerLocaleData(localeDe);
registerLocaleData(localeFr);

translateService.setDefaultLang(lang.replace(/\-/, '_'));
}
}
4 changes: 0 additions & 4 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* SystemJS module definition */
declare var module: NodeModule;

declare module '@angular/common/locales/global/de';

declare module '@angular/common/locales/global/fr';

interface NodeModule {
id: string;
}
Expand Down
10 changes: 0 additions & 10 deletions templates/webpack/webpack.custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,6 @@ export default (
return locale.replace('_', '-');
}

// move Angular locale data into bundle with translations
const localeDataMatch = /[\\/]@angular[\\/]common[\\/]locales[\\/]global[\\/](.*?)\.js/.exec(identifier);
let localeData = localeDataMatch && localeDataMatch[1];
if (localeData) {
if (!localeData.includes('-')) {
localeData += '-' + localeData.toUpperCase();
}
return localeData;
}

const match = /[\\/](extensions|projects)[\\/](.*?)[\\/](src[\\/]app[\\/])?(.*)/.exec(identifier);
const feature = match && match[2];

Expand Down

1 comment on commit aab383f

@github-actions
Copy link

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:

Please sign in to comment.