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.

Sometimes DefaultLang translations are not loaded when starting the app

See original GitHub issue

Current behavior

This issue may be similar to: https://github.com/ngx-translate/core/issues/1162.

In my case I noticed that the app, while in production, sometimes fails to fallback to the default language on the first loaded screen (i.e. app first startup on load).

For example:

  1. Main resources file is ‘en.json’. There is also an additional ‘es.json’ which is empty ‘{}’.
  2. When the app loads it sets ‘en’ as the default language: this.translate.setDefaultLang('en');
  3. The app upon startup sets the current lang to ‘es’: this.translate.use('es').
  4. In this case, sometimes, the fallback to ‘en’ fails and I see the resource key instead.

Some observations:

  1. If the app sets the language to be ‘en’ on startup, I don’t see the problem reoccur.
  2. If the es.json file is not empty but has a translation, the problem does not occur.

For these reasons I suspect that the problem is with falling back to the default language when a resource is missing.

I tried various solutions to delay loading and ensure the translations are available but unfortunately could not workaround these problems. Here are some of the things I tried:

Expected behavior

The default language should be used for missing keys in the selected language, also upon application startup.

Environment


ngx-translate version: 13.0.0
Angular version: 12.1.0

Browser:
- Chrome (desktop) version 95
 
For Tooling issues:
- Node version: 14.17.1
- Platform:  win32 x64

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
rbaletcommented, Nov 18, 2021

Hi @ewolfman. I had the same problem, which came funnily from my httpInterceptor.

I was using the angular http to load my file, but it was then delayed by my httpInterceptor, which then had created a problem while instantiating the TranslateService.

Here’s how I fixed it.

1. I recreated the MultiTranslateHttpLoader to be able to add the X-Skip-Interceptor

export class MegaphoneMultiTranslateHttpLoader implements TranslateLoader {
  constructor(private http: HttpClient, private resourcesPrefix: string[]) {}

  public getTranslation(lang: string): Observable<any> {
  
  
    const headers = new HttpHeaders() // <-- Skipping httpInerceptor
      .set('X-Skip-Interceptor', '')
      .set('Content-Type', 'application/json; charset=utf-8')

    const requests = this.resourcesPrefix.map((prefix) => {
      const path = `${prefix}${lang}.json`

      return this.http.get(path, { headers }).pipe(
        catchError((res) => {
          console.group('MegaphoneMultiTranslateHttpLoader')
          console.error('Something went wrong for the following translation file:', path)
          console.error('Content of the error: ', res)
          console.groupEnd()
          return of({})
        }),
      )
    })

    return forkJoin(requests).pipe(map((response) => merge.all(response)))
  }
}

2. Skip it in the httpInterceptor

Here, I listen to the headers, if it does have the InterceptorSkipHeader, skip it

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
  const InterceptorSkipHeader = 'X-Skip-Interceptor'

    if (request.headers.has(InterceptorSkipHeader)) {
      const headers = request.headers.delete(InterceptorSkipHeader)
      return next.handle(request.clone({ headers }))
    }
  // ...
}

@ocombe I think this could be communicated somewhere in the wiki

0reactions
rbaletcommented, Oct 31, 2022

@ocombe Feel free to close this issue since I took over the library that was causing the problem & fixed the problem there

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx-translate sometimes not load json on init - Stack Overflow
I have an angular application with 3 languages: Spanish, English and Catalan. The app is deployed in Production,and we are facing a very ......
Read more >
ngneat-transloco/lobby - Gitter
Iam kinda new to angular and id like to have translations in my app. ... is it normal to sometimes have the files...
Read more >
Internationalizing your Angular app - Briebug Blog
It includes schematics, supports Ivy, testing, supports multiple fallbacks, lazy loading translations, and even Universal. Implement Transloco.
Read more >
Introducing Transloco: Angular Internationalization Done Right
It's responsible for instructing transloco how to load the translation files. ... Sometimes you may need to translate a key in a component...
Read more >
Google Translate Community
Trending posts ; Anyone aware that after the Android 10 roll-out The Tap to translate no longer will display over app · 3...
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