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

Is there a way to not fail on invalid translations, but fallback in non-breakable maner #21

Closed
sidoruk-sv opened this issue Apr 1, 2020 · 4 comments · Fixed by #27
Closed

Comments

@sidoruk-sv
Copy link
Contributor

sidoruk-sv commented Apr 1, 2020

For keys, where some invalid key or non-passed variable is used, all the application is broken with an unhandled exception.

For example:

  1. key with minus for unescape, that is valid for i18next without ICU, but will fail your production when using with ICU plugin:

Example key:

{
  "test_unascaped_variable__key": "some {- key}"
}

Runtime exception:

  SyntaxError: Expected "," or "}" but "k" found.

      at peg$buildException (../../../../../src/parser.js:363:14)
      at Function.peg$parse (../../../../../src/parser.js:1382:13)
      at new MessageFormat (../../../../../src/core.js:21:27)
      at ICU.parse (node_modules/i18next-icu/dist/commonjs/index.js:120:14)
      at Translator.extendTranslation (node_modules/i18next/dist/cjs/i18next.js:708:31)
      at Translator.translate (node_modules/i18next/dist/cjs/i18next.js:692:20)
      at I18n.t (node_modules/i18next/dist/cjs/i18next.js:2139:80)
      at fixedT (node_modules/i18next/dist/cjs/i18next.js:2122:23)
      at TestComponentInvalidKey (src/TranslateProvider/TranslateProvider.driver.tsx:65:48)
  1. non-passed variable also works great (is ignoring) with i18next
{
	"test_empty_variable": "some absent here {variable}"
}

but also causing the runtime exception

The intl string context variable 'variable' was not provided to the string 'some absent here {variable}'

      at MessageFormat.format (../../../../../src/core.js:48:17)
      at ICU.parse (node_modules/i18next-icu/dist/commonjs/index.js:124:17)
      at Translator.extendTranslation (node_modules/i18next/dist/cjs/i18next.js:708:31)
      at Translator.translate (node_modules/i18next/dist/cjs/i18next.js:692:20)
      at I18n.t (node_modules/i18next/dist/cjs/i18next.js:2139:80)
      at fixedT (node_modules/i18next/dist/cjs/i18next.js:2122:23)
      at TestComponentInvalidKey (src/TranslateProvider/TranslateProvider.driver.tsx:66:51)

It is OK to fix those errors one time to not getting exceptions on production when it is a small project.

But when the translation process is separated from development, it could be harmful that localization could break the whole application.

So as a fallback showing translated key, without interpolated invalid value (so showing in UI some absent here {variable}) is better, then failing with an exception. And showing React error boundaries errors for every place where translations are affected (could be in any key, any language).

Otherwise, we had to add some tests for checking translation files somehow (need to be invented), before using them.

@jamuhl
Copy link
Member

jamuhl commented Apr 1, 2020

Feel free to provide a PR

@mihanizm56
Copy link

The same problem - the whole app fails if one parameter was not provided to the translation =(

@jamuhl
Copy link
Member

jamuhl commented Apr 30, 2020

@mihanizm56 why not providing a PR doing a try/catch somewhere around here: https://github.com/i18next/i18next-icu/blob/master/src/index.js#L79

@mihanizm56
Copy link

@mihanizm56 why not providing a PR doing a try/catch somewhere around here: https://github.com/i18next/i18next-icu/blob/master/src/index.js#L79

Not so useful to wrap lib methods into try/catch

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

Successfully merging a pull request may close this issue.

3 participants