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

Questions/Feature Request: "polluting" components / translate pipe without language parameter #46

Closed
mscudlik opened this issue Jan 7, 2017 · 5 comments

Comments

@mscudlik
Copy link

mscudlik commented Jan 7, 2017

I use your great localization library accross multiple modules and components and setup everything in my AppModule including setting the locale/language like described [here] + setCurrentLocale().(https://github.com/robisim74/angular2localization/blob/master/doc/quick-start.md)

So far, so good, now my questions regarding using it:

  1. Do i have to pollute" all my components with extends locale and so on? It seems to work without it which i would highly appreciate

  2. If i don't have to pollute my components, there is an issue with the lang parameter of the translate pipe:

  • ideally i just want to do this: {{ 'foo' | translate }} so that the language that has been setup on bootstrap is used. But currently the texts are not translated and i get an empty output
  • using {{ 'foo' | translate:lang }} i have two options:
    • get the current lang parameter from the locale service --> pollutes my code again :(
    • just dont provide the lang property in my component.ts, so that it is null and the fallback is used
      • unfortunately tslint will tell me that i use an invalid property :(
  • my current solution is to turn off the tslint warning, which is also not so good.

Can you make the lang parameter optional so that the default is beeing used? Might also be related to #45

Is there already a working solution for this?

Thanks and best regards,
Matthias

@robisim74
Copy link
Owner

robisim74 commented Jan 7, 2017

Hi Matthias,

  1. Yes: because pipes are pure you have to extend Locale class that provides the values for lang and defaultLocale parameters. It's normal to use inheritance in OOP, and it isn't a pollution.
    Alternatively, you can use Html attributes: https://github.com/robisim74/angular2localization/blob/master/doc/spec.md#2.4 (and you can use the pipes only when you can not use Html attributes, as with the placeholders etc.)
    To know why this project is passed to the pure pipes, see this post: Advanced initialization #33

  2. I don't know a way to use only {{ 'foo' | translate }} using pure pipes: I tried in every way, without success.
    About TSLint/Codelyzer, I have already opened this issue: no-access-missing-member and extended classes mgechev/codelyzer#191

The value of lang is already optional (not the parameter), and the default language is already used: but without it, and without extending the Locale class:

  • you have to use Advanced initialization to make sure the app is rendered after the http request of the json files is completed
  • you can't change language at runtime (you need a new bootstrap of the application)

Greetings,

Roberto

@mscudlik
Copy link
Author

mscudlik commented Jan 8, 2017

Hi Roberto,

thanks a lot for explaing things helping me out :)

  1. i know that inheriting is not a pollution in general, but i think it kind of restricts me from having my own super class and it is "a lot" of boilerplate code (if you have to do it in a lot of components). So thanks a lot for the hint with the HTML attributes, i think i'll go this way whereever i can. One thing that came up when i experimented with the HTML attributes:

i had to use

<div translate>foo</div>

and this didn't work:

<div translate>
    foo
</div>

Probably because of the spaces - if there is no reason to keep it this way you might consider to trim the white spaces :)

  1. I'm not an expert creating own pipes and not sure why you used impure before, but since you already have the services available in the pipe - why not just use the value from the service?

Can you provide an example with Advanced Initialization or is this what you mean with it? I'd rather not "code" the translation, on the other hand i could just use seperate .ts files instead of json for that

Thanks and best regards,
Matthias

@mscudlik
Copy link
Author

mscudlik commented Jan 8, 2017

Just saw #33, sorry :-)

@robisim74
Copy link
Owner

No problem.

  1. About the Html attributes, I'll evaluate to trim the white spaces in the next release. Thanks for the tip!

  2. We can't use the value from the service because the transform method of a pure pipe is invoked by Angular Change Detection mechanism only when the input parameters change.

Greetings,

Roberto

@robisim74
Copy link
Owner

I'm closing it. If you need, open a new issue. Greetings

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

No branches or pull requests

2 participants