gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
159 stars 37 forks source link

Fix: Add support to custom t names on custom translation hooks #276

Open guiedugt opened 2 months ago

guiedugt commented 2 months ago

Using custom t names combined with custom translation hooks is not working as expected. For example, the translation keys will not be extracted:

function MyComponent() {
  const { myTranslationFunction } = useMyTranslationHook('namespace');

  return <p>{myTranslationFunction('myTranslationKey')}</p>;
}

After some investigation, I found this is due to a hardcoded "t" key inside the useTranslationHook extractor:

https://github.com/gilbsgilbs/babel-plugin-i18next-extract/blob/7cc5b807de142bedabcacb234ecc167e9db343a1/src/extractors/useTranslationHook.ts#L59

This PR adds some simple logic to try out different function names in the order they are defined in the tFunctionNames configuration parameter