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

Add backquote support #57

Closed
Kudo opened this issue Nov 22, 2017 · 5 comments
Closed

Add backquote support #57

Kudo opened this issue Nov 22, 2017 · 5 comments

Comments

@Kudo
Copy link

Kudo commented Nov 22, 2017

Version

  • i18next: 10.0.7
  • i18next-scanner: 2.1.2

Configuration

{ // i18next-scanner options
    defaultValue: '__STRING_NOT_TRANSLATED__',
    resource: {
      loadPath: 'i18n/{{lng}}/{{ns}}.json',
      savePath: 'i18n/{{lng}}/{{ns}}.json',
      jsonIndent: 2
    },
    nsSeparator: false,
    keySeparator: false
}

For multiline string, I sometimes use ES6 template strings and it seems i18next-scanner does not support backquote according to the test case.

My test case is:

i18next.t(`aString`);

or even further

i18next.t(`line1
  line2
  line3`);

Could you please help to add the backquote parsing support?
Thank you.

@cheton
Copy link
Member

cheton commented Nov 22, 2017

The assumption for using template literals is to embed expressions within a string, however, it's not possible for i18next-scanner to find the translation key using static analysis.

I can add support for matching back-tick (` `) character, but it would need user's help to specify an option like defaultKey, as either below:

  • defaultKeys as an array of translation keys
i18next.t(`added ${something}`, {
    defaultKeys: [
        'added foo',
        'added bar'
    ]
});
  • defaultKey as an object containing all the variables
i18next.t(`added ${something}`, {
    defaultKey: {
        'something': ['foo', 'bar']
    }
});

@Kudo
Copy link
Author

Kudo commented Nov 22, 2017

The interpolation mechanism in i18next works well.
My case is just to use template strings for multiline strings and we don't need the replace the content.
I.e. I will do something like that from your case.

i18next.t(`added {{something}}
  and {{bar}}`, { something: 'foo', bar: 'bar' });

Well, adding this feature may confuse new user especially between the partial template strings support and interpolation.
Please consider and if there are any concerns just forget me and I will close this issue.

@cheton
Copy link
Member

cheton commented Nov 22, 2017

Sure. I will update the parser to support back-tick (` `) characters, just to let you know the translation key will have newline characters like below:

{
    "added {{something}}\n and {{bar}}": ""
}

cheton added a commit that referenced this issue Nov 22, 2017
@cheton
Copy link
Member

cheton commented Nov 22, 2017

Published i18next-scanner@2.1.3

@cheton cheton closed this as completed Nov 22, 2017
@Kudo
Copy link
Author

Kudo commented Nov 23, 2017

It works like a charm. Thank you so much for quick enhancement.

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