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.

Add `langChanged$` observable

See original GitHub issue

Is your feature request related to a problem? Please describe.

TranslocoService provides an observable that emits when the language is about to change. But there is no observable that emits when the language has changed.

Current behavior

When using langChanges$ to trigger rerendering a pipe the pipe rerenders but the language has not been loaded yet and therefor the pipe rerenders in the same language as before. This would be useful for https://github.com/ngneat/transloco/issues/443

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
gabriel-gncommented, Nov 30, 2021
3reactions
mfodorcommented, Jul 9, 2021

I face with the very same problem in our project as well. I’ve already tried with events$ which can help when the first lang change happens, but it is not fired after that.

I need to translate computed keys programmatically in the controller. So this is working fine now:

    this.transloco.langChanges$.subscribe(() => {
      this.buildHRParams();

      // this is needed to fix translation when the other language is loaded for the first time and so is not available
      // but is not enough since this is never called again later
      this.transloco.events$.pipe(
        filter(e => e.type === 'translationLoadSuccess'),
        pluck('payload'),
        first()
      ).subscribe(() => {
        this.buildHRParams();
      });
    });

But is this really a good API to use? Why should I call buildHRParams() twice? Why should I subscribe in a subscription for the (kind of the) same event?

langChanges$ is fired before loading the other language file
events$ fired only when real loading happened and not the cache has been used. We could say that it’s “bug” should be fixed and fire a translationLoadSuccess event even when loaded from the cache but I’d still say that why should I do 2 subscription for the 1 event I am interested of.

It’s easy to see that there is a valid need to have a langChanged$ event which is fired always after having the new translations available.

I don’t know if there is any reason to fire the langChanges$ event before loading the translations but if there is none then even that could be changed to be fired after the loading.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation API | Transloco Angular i18n - GitHub Pages
Returns an observable that when subscribed loads and emits the given language translation object, if no language is passed the active language will...
Read more >
How to Nest Multiple Observables/Subscriptions
And the subscription as I have it: this.langChanged .subscribe(newLang => { console.log( ...
Read more >
ngneat-transloco/lobby - Gitter
langChanges $ | async) }} , i.e. I pass the langChanges observable as an async parameter to my pipe, ... I installed it...
Read more >
[Solved]-How to Nest Multiple Observables/Subscriptions-rx.js
langChanged .subscribe(newLang => { console.log(`language changed ... I also added a second Subject cancelLangChange which is meant to cancel the forkJoin ...
Read more >
Change the language of your Angular components in ...
npx @angular/cli add @ngneat/transloco --langs en,bg,es ... Translation } from "@ngneat/transloco" import { Observable } from "rxjs" ...
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