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

feat: support aliased t functions from useTranslation hooks #278

Merged
merged 2 commits into from
Dec 7, 2024

Conversation

masnormen
Copy link
Contributor

In my codebase, there are a lot of cases where we use aliased t function to differentiate between 2 useTranslation hook calls referring to different namespaces. (bcs of typescript perf issue as described in: i18next/i18next#2198)

function MyComponent() {
  const { t: tCommon } = useTranslation('common');
  const { t: tProfile } = useTranslation('profile');

  return <p>{tCommon('key1')} {tProfile('key2')}</p>;
}

The keys could be detected if we add tCommon and tProfile into the tFunctionNames options. Unfortunately, it can't detect the namespace and keyprefixes correctly. The t function call is detected as an "orphan" function call (as in, not originating from the hook).

Because the way useTranslationHook extractor works is using a hardcoded "t", therefore the plugin couldn't identify if the t function has been aliased.

const tBinding = id.scope.bindings['t'];

This PR enables the plugin to detect if the t function has been aliased.

This PR also includes fixes and tests from #276 as the changed code is related. Credit to @guiedugt. Sorry for just copy pasting. I want to include their commits so they're included as Author in this PR, but I couldn't find a way to merge/include changes from a different fork repo.

This project will be very useful for us the moment this PR being merged. Thank you for creating this project!

@gilbsgilbs gilbsgilbs self-assigned this Dec 7, 2024
@gilbsgilbs
Copy link
Owner

gilbsgilbs commented Dec 7, 2024

Looks like a beautiful and well-tested PR, thank you. Indeed, I completely missed #276 for some reasons, I'm very sorry @guiedugt.

@masnormen Would you mind rebasing this branch against his so that commit authorship is preserved, and squash your commits to make my review easier? Also, please add trailing newlines on files where they're missing (I thought prettier should have handled that, but maybe not). I'll approve just to let the CI run.

gilbsgilbs
gilbsgilbs previously approved these changes Dec 7, 2024
@gilbsgilbs gilbsgilbs force-pushed the feat/aliased-t-function branch from 2cc4812 to 5f47b46 Compare December 7, 2024 20:34
@gilbsgilbs gilbsgilbs merged commit 6ad571b into gilbsgilbs:master Dec 7, 2024
6 checks passed
@gilbsgilbs
Copy link
Owner

Thanks! I'll try to do some maintenance chore, and draft a release tonight or tomorrow.

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 this pull request may close these issues.

3 participants