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: parse namespace from t type arguments #701

Merged
merged 2 commits into from Jan 16, 2023

Conversation

ggalletty
Copy link
Contributor

@ggalletty ggalletty commented Nov 21, 2022

Why am I submitting this PR

It allows us to use type argument of t function (see type definition) to set default namespace when parsing content.

This is particularly useful when you are passing t from component, for example

// component.tsx
export const UserDetail = () => {
  const { t } = useTranslation("user");
  const templateData = useMemo(() => getData(t), [t]);
  return <UserProfile help={t("help.tooltip")} content={templateData} />
};

// util.ts
export const getData = (t: TFunction<"user">) => ({
  greeting: t("user.greeting", "Welcome!"),
  intro: t("user.body"),
})

Even though the example above might not be the best use case, it should be good enough to explain how we can benefit from this.

In the case above, you'd normally end up with one translation in user namespace and the rest in the default namespace.
With the proposed changes, we would parse the function declarations to extract namespace from type arguments of t so that all parsed keys end up in user namespace.

What do you guys think about this? Would it be somewhat helpful? Have you found yourselves dealing with this kind of cases in the past?

Does it fix an existing ticket?

Yes #703

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • tests are included and pass: yarn test (see details here)
  • documentation is changed or added

@karellm karellm merged commit d6a956e into i18next:master Jan 16, 2023
@karellm
Copy link
Member

karellm commented Jan 16, 2023

Thanks ! This is published as 7.3.0

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.

None yet

2 participants