Core: Improve .get() to update whenever language is being changed with .use()

Created on 21 Nov 2016  路  12Comments  路  Source: ngx-translate/core

I'm submitting a ... (check one with "x")

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request

Current behavior
Whenever I change language, parts in my code where I use .get doesn't change. I am aware of onLangChange, but that wouldn't make sense for me to use it.

Expected/desired behavior
It should call again on subscribtion with new languages translations. I assume that observable has completed and that is why it is not calling anymore(haven't looked at source).

Reproduction of the problem
Example code of what I have now (using with ng2-breadcrumbs):

    this.translate.get(['pages.user.title', 'menus.user.profile']).subscribe((res) => {
      this.breadcrumb.addFriendlyNameForRoute('/users', res['pages.user.title']);
      this.breadcrumb.addFriendlyNameForRoute('/users/edit', res['menus.user.profile']);
    });

If I would use .onLangChange code would look something like this:

    this.translate.get(['pages.user.title', 'menus.user.profile']).subscribe((res) => {
      this.breadcrumb.addFriendlyNameForRoute('/users', res['pages.user.title']);
      this.breadcrumb.addFriendlyNameForRoute('/users/edit', res['menus.user.profile']);
    });

    this.translate.onLangChange.subscribe(() => {
      this.translate.get(['pages.user.title', 'menus.user.profile']).subscribe((res) => {
        this.breadcrumb.addFriendlyNameForRoute('/users', res['pages.user.title']);
        this.breadcrumb.addFriendlyNameForRoute('/users/edit', res['menus.user.profile']);
      });
    });

Please tell us about your environment:

  • ng2-translate version: 4.0.1

  • Angular version: 2.2.1

  • Browser: [all]

  • Language: [all]

medium TODO feature request

Most helpful comment

This would drastically simplify a lot of our components and I'll even send a PR for it to - hopefully - get it into a release. Since changing the current get would silently lead to memory leaks I think adding a new function would be less destructive.

Any ideas for the function name? I'll send the PR in the meantime but will change the name to whatever's suggested here.

All 12 comments

Or you could do:

    this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
        this.breadcrumb.addFriendlyNameForRoute('/users', event.translations['pages']['user']['title']);
        this.breadcrumb.addFriendlyNameForRoute('/users/edit', event.translations['menus']['user.profile']);
    });

A LangChangeEvent is an object with the properties lang: string & translations: any (an object containing your translations).

But I understand what you mean, you'd like an observable that would be like get but permanent, I'll see what I can do

I tried that yesterday(night), but it didn't work on first load, for example, my app loads - I set the language, then I decide to navigate to users page(where that onLangChange is), but the code hasn't been executed, because setting the language happened before the onLangChange was executed. That is why my 2nd example.

Ahhh good point.

Should I change get behavior in order to not complete automatically, or should I add a new method? Any idea for the new method?

I'm not sure, wouldn't it break something if you change the get? Adding new method or giving extra param to get would be amazing. Implementation is up to you as you know the code structure better. :slightly_smiling_face:

Should I change get behavior in order to not complete automatically

Does this mean the user would have to unsubscribe manually to prevent memory leaks?

@SamVerschueren I assume that would be the same thing as with onLangChange - would require unsubscribing.

@ocombe Any progress on this?

Not yet, but should be in the next major

This would drastically simplify a lot of our components and I'll even send a PR for it to - hopefully - get it into a release. Since changing the current get would silently lead to memory leaks I think adding a new function would be less destructive.

Any ideas for the function name? I'll send the PR in the meantime but will change the name to whatever's suggested here.

:O Great.

I'm merging a few other PRs and I'll release a new version

Looking forward to see changelog :)

Was this page helpful?
0 / 5 - 0 ratings