Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve .get() to update whenever language is being changed with .use() #330

Closed
GuskiS opened this issue Nov 21, 2016 · 12 comments
Closed

Comments

@GuskiS
Copy link

GuskiS commented Nov 21, 2016

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]

@ocombe
Copy link
Member

ocombe commented Nov 21, 2016

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

@GuskiS
Copy link
Author

GuskiS commented Nov 21, 2016

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.

@ocombe
Copy link
Member

ocombe commented Nov 21, 2016

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?

@GuskiS
Copy link
Author

GuskiS commented Nov 21, 2016

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. 🙂

@SamVerschueren
Copy link
Contributor

SamVerschueren commented Nov 21, 2016

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?

@GuskiS
Copy link
Author

GuskiS commented Nov 21, 2016

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

@GuskiS
Copy link
Author

GuskiS commented Dec 2, 2016

@ocombe Any progress on this?

@ocombe
Copy link
Member

ocombe commented Dec 21, 2016

Not yet, but should be in the next major

@zsparal
Copy link
Contributor

zsparal commented Apr 28, 2017

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.

@GuskiS
Copy link
Author

GuskiS commented May 24, 2017

:O Great.

@ocombe
Copy link
Member

ocombe commented May 24, 2017

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

@GuskiS
Copy link
Author

GuskiS commented May 24, 2017

Looking forward to see changelog :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants