question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing translation on the client side with the angular universal starter app

See original GitHub issue

I’m submitting a … (check one with “x”) [X ] support request => check the FAQ and search github for a similar issue before submitting

Current behavior

I try without success since a half day to use ngx-translate with the angular universal starter app https://github.com/angular/universal-starter.

When I start npm start my server and reload my pages I see briefly that the translation is found before being replaced with the key which traditionally show that something doesn’t work with ngx-translate. Also when I change the path of the translation in the server-app.module.ts I see an error in the console of the server, therefore I think that my server part is alright and that the problem comes from the client side.

app.module.ts:

 export function exportTranslateStaticLoader(http: HttpClient) {
    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
 }
 
 imports: [
    TranslateModule.forRoot({
    loader: {
      provide: TranslateLoader,
      useFactory: exportTranslateStaticLoader,
      deps: [Http]
    }
  }
  )
]

browser-app.module.ts:

 imports: [TranslateModule.forChild()]

server-app.module.ts:

  export function translateFactory() {
    return new TranslateUniversalLoader('./dist/assets/i18n', '.json');
  }

  imports: [
      TranslateModule.forRoot({
         loader: {
          provide: TranslateLoader,
           useFactory: translateFactory
      }
    })
 ]

TranslateUniversalLoader:

 import {Observable} from "rxjs/Observable";
 import {TranslateLoader} from '@ngx-translate/core';
 const fs = require('fs');
 
 export class TranslateUniversalLoader implements TranslateLoader {
   constructor(private prefix: string = 'i18n', private suffix: string = '.json') {}
     
   public getTranslation(lang: string): Observable<any> {
     return Observable.create(observer => {
 observer.next(JSON.parse(fs.readFileSync(`${this.prefix}/${lang}${this.suffix}`, 'utf8')));
       observer.complete();
     });
   }
 }

webpack.common.js:

 plugins: [
new copyWebpackPlugin([
     {
       from: './src/assets/i18n/en.json',
       to: './assets/i18n/en.json'
     }
   ])
 ]   

app.component.ts:

 ngOnInit() {
     this.translateService.setDefaultLang('en');
     this.translateService.use('en');
 }

Futhermore, when I query http://localhost:8000/assets/i18n/en.json I get a valid answer back respectively my en.json:

{
   "TEST": "Super super"
 }

What am I missing???

P.S.: Same question on https://stackoverflow.com/questions/45490937/ngx-translate-and-angular-universal-client-side-translations-not-found

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
crebuhcommented, Aug 15, 2017

@peterpeterparker

weird i had to remove the import of TranslateModule.forChild() in the browser.app-module then it worked. thanks for your detailed instructions!

0reactions
peterpeterparkercommented, Aug 15, 2017

@crebuh yes me too, had to remove the TranslateModule.forChild() in the browser.app-module.ts…still don’t understand why

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Universal with i18n (Server Side Rendering)
Hi, I'm trying to do this but I don't want to use *. · No I'm using ng2-translate because I didn't find a...
Read more >
MissingTranslationStrategy
Use this enum at bootstrap as an option of bootstrapModule to define the strategy that the compiler should use in case of missing...
Read more >
Implementing multi-language Angular applications ...
Run the application and notice that it's using the translations from the en.json file. Let's add a component that will let us switch...
Read more >
All side optimized Next.js translations (a next-i18next guide)
Optimize your Next.js app to best work with translations on server side and on client side with next-i18next.
Read more >
next-i18next
The easiest way to translate your NextJs apps. ... Start using next-i18next in your project by running `npm i next-i18next`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found