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

i18next escapes HTML special characters in substitutions by default #43

Open
Finesse opened this issue Jan 31, 2020 · 2 comments
Open
Assignees

Comments

@Finesse
Copy link

Finesse commented Jan 31, 2020

This is not a problem in angular-i18next. But I think you can improve the usage experience by considering this i18next feature in any way. It is a problem because Angular escapes special characters too which leads to double-escaping. For example, this code {{ 'greeting' | i18next: { name: '"Google"' } }} gives this string in browser Hello, "Google"!. You can mention this feature in the readme and offer/implement one of the solutions:

  • Disable the escaping globally in the i18next configuration:

    i18next.init({
      // ...,
      interpolation: {
        escapeValue: false,
      },
    });

    This is the best solution IMHO because dealing with a raw HTML in Angular is a rare case and one who deals with raw HTML would need to escape the substitutions manually anyway.

  • Disable the escaping in the pipe or/and in the service:

    i18next.t(key, {
      ...substitutions,
      interpolation: {
        escapeValue: false,
        ...substitutions.interpolation,
      },
    })
  • Disable the escaping in templates: greeting: 'Hello, {{- name}}!'

@Romanchuk Romanchuk self-assigned this Feb 17, 2020
@Jeremias-Nater
Copy link

For people stumbling uppon this on google: a workaround is to use the anguler [innerHtml]="'translateMe' | i18next" attribute.

@Jeremias-Nater
Copy link

Jeremias-Nater commented Feb 24, 2022

to revert encoding of characters like & you can use the following function, which uses the browser native behaviour:

export function i18nUnescape( escapedString: string ): string {
  const el = document.createElement( 'div' );
  el.innerHTML = el.textContent = escapedString;
  const unescapedString = el.innerText;
  return unescapedString;
}

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

3 participants