i18next / i18next-parser

Parse your code to extract translation keys/values and manage your catalog files
MIT License
473 stars 195 forks source link

Support custom namespaced functions and components #912

Closed taneliang closed 11 months ago

taneliang commented 1 year ago

🐛 Bug Report

In our codebase, we'd like to re-export i18n functions from a common namespace, i.e. instead of using t, i18next.t, or <Trans>, we'd like to use something like Strings.get and <Strings.Text>. Although the JS and JSX lexers support custom functions and componentFunctions, they assume that the functions and components are TS identifiers, and are not able to match the names of property accessors.

To Reproduce

Given this config:

module.exports = {
  lexers: {
    js: [
      {
        lexer: 'JavascriptLexer',
        functions: ['strings.for'], // Setting this to `'for'` works, but this will also match other functions named `for`, including `Symbol.for`.
      },
    ],
    jsx: [
      {
        lexer: 'JsxLexer',
        functions: ['strings.for'],
        componentFunctions: ['strings.Text'],
      },
    ],
  },
}

And this code:

Strings.get('key1');
<Strings.text i18nKey="key2" />

Expected behavior

Both keys should be matched and extracted.

{
  "key1": "",
  "key2": ""
}

Your Environment

karellm commented 11 months ago

Fixed as of 8.8.0