Angular universal + @ngx-translate
This guide is a sequel to this story
Refactoring the project
We need to split up the AppModule in a seperate browser/server module
- Install ngx-translate
npm i @ngx-translate/core@13 @ngx-translate/http-loader@6
Copy the file app.module.ts
as app.browser.module.ts
and make the following changes
Now instead of bootstrapping the appmodule, we need to bootstrap this module:
Adding ngx-translate
Import the TranslateModule
into the AppBrowserModule
like this:
As you can see, we’re using the TranslateHttpLoader
here.
Import the TranslateModule
into the AppServerModule
like this:
This loader just pulls the json from your translation files and returns it as an object.
Don’t forget the app.module.ts
Create translation files
We still have to turn on resolveJsonModule
Now let’s show this message in the AppComponent:
Change language
Now listen to the queryparams and call the TranslateService
to change the language:
Now add 2 links to the toolbar to change the lang
queryparameter
At last let’s make the link texts visible. Add the following scss
Build the project
In case you disabled the BootModuleBuilder
, run the following command manually:
npm run build:ssr
Success:
You can view the changes in this commit