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

Directive approach does not work #439

Closed
stevermeister opened this issue Feb 21, 2017 · 12 comments
Closed

Directive approach does not work #439

stevermeister opened this issue Feb 21, 2017 · 12 comments

Comments

@stevermeister
Copy link

stevermeister commented Feb 21, 2017

I'm submitting a ...

[ x ] bug report
[ ] support request
[ ] feature request

Current behavior
Directive approach
<span [translate]="HELLO'"></span>
does not update the text when I switch to another language.
http://stackoverflow.com/questions/42349738/ngx-translate-pipe-vs-directive

plunker - https://plnkr.co/edit/PykLBrZGI1jz7Imsnzz6?p=preview

@Verurteilt
Copy link

Verurteilt commented Feb 21, 2017

Pretty weird, I think this problem could be related to the checkNodes function inside the directive, anyway, if you change your code to this one, it works:

<h2 translate>HOME.TITLE</h2>

Plunker: https://plnkr.co/edit/AKy99ttZz63Rht4tufby?p=preview

@stevermeister
Copy link
Author

stevermeister commented Feb 22, 2017

I know about the option
<h2 translate>HOME.TITLE</h2>

but actually the idea, why I want to use exactly directive approach is I still want to keep original text in template, i.e.:

<span [translate]="HELLO'">Hello Dear User</span>

@xtgrant
Copy link

xtgrant commented Feb 27, 2017

Hi @stevermeister
I'm having the same issue. When using directive as attribute with value,
<span [translate]="HELLO'">
will not work because the checkNodes function only queues elements with text length > 0.
Because <span> has no text length, the checkNodes method will not go into the for loop.

From DOM childNodes method documentation::
Whitespace inside elements is considered as text, and text is considered as nodes. Comments are also considered as nodes

If you add a space in the <span> </span>, it will work.
The space gives <span> a length of 1 and it goes into the loop.

This is in fact a bug. There should be a case where if childNodes length is empty, it should still render text.

@stevermeister
Copy link
Author

stevermeister commented Feb 27, 2017

@xtgrant

you probably did not get my point, I have more than space in between:

<span [translate]="'HELLO'">Hello Dear User</span>

and it still does not work when I change the language

@xtgrant
Copy link

xtgrant commented Feb 27, 2017

Oh, if this code sample is from your actual code, then it's your syntax. you are missing single quote before the HELLO.
<span [translate]="'HELLO'">

@stevermeister
Copy link
Author

I provided a plunker with exact code, you can check there. Quotes are on their place

@xtgrant
Copy link

xtgrant commented Feb 27, 2017

Found this:
#355

So, it's not related. The directive selectors were already given the type @input.
Maybe its isolated scope, with that syntax, and only applies once on load.

@stevermeister
Copy link
Author

yes, #355 looks similar.

it should do the check
https://github.com/ngx-translate/core/blob/master/src/translate.directive.ts#L85

I think I need to dive in code myself, otherwise I'll never get an answer here :)

@sGerli
Copy link

sGerli commented Mar 5, 2017

I'm having the same issue with directive, they aren't updating.

@f1ssl
Copy link

f1ssl commented Mar 9, 2017

Had the same problem, but

<span [translate]>HELLO</span>

did it for me.

But i think this is not the total solution. Will there be a bugfix concerning this issue?

@stevermeister
Copy link
Author

ideally I would like to have response from @ocombe for this issue

@ocombe
Copy link
Member

ocombe commented Mar 23, 2017

Seems like a bug to me: when the key is forced it doesn't detect changes because the key is always the same...
We should always set node.lastKey = null; when forceUpdate is true in the checkNodes method for the directive

@ocombe ocombe closed this as completed in 8753085 Mar 23, 2017
ocombe added a commit that referenced this issue Mar 23, 2017
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

6 participants