Skip to content

Translate directive whitespace handling can cause erroneous recursive translation #998

@dpmott

Description

@dpmott

Current behavior

An HTML element with a translate directive and whose content is a translation key with leading/trailing spaces/newlines will return an erroneous recursive translation when the en.json file has an additional key matching the VALUE of the referenced translation key.

Example HTML:

<div translate>
  NAV_BAR.LINK_TEXT
</div>

Example en.json:

  "NAV_BAR": {
    "LINK_TEXT": "EXAMPLE"
  },
  "EXAMPLE": {
    "THIS": "this",
    "THAT": "that"
  }

With this example setup, the <div> will display [object Object].

Note that the following HTML will not trigger the bug:
<div translate>NAV_BAR.LINK_TEXT</div>

Expected behavior

The <div> should display EXAMPLE.

How do you think that we should fix this?

On this line: https://github.com/ngx-translate/core/blob/master/projects/ngx-translate/core/src/lib/translate.directive.ts#L82

Change if (content!== node.currentValue)
to
if (trimmedContent !== node.currentValue)
because
https://github.com/ngx-translate/core/blob/master/projects/ngx-translate/core/src/lib/translate.directive.ts#L115
explicitly preserves whitespace in the node content, so only the trimmed content must be treated as the key.

Minimal reproduction of the problem with instructions

https://github-ghfrtw.stackblitz.io

Environment


ngx-translate version: 10.0.2
Angular version: 6.1.10

This is still an issue in the latest ngx-translate version.
This was not an issue in ngx-translate 8.0.0

Browser:
- [X ] Chrome (desktop) Version 71.0.3578.98 (Official Build) (64-bit)
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

(Presumably other browsers as well; I didn't check them.)
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions