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

Reading context dynamically per tFunction, TransComponent #243

Open
jlee0425 opened this issue Jan 26, 2023 · 0 comments
Open

Reading context dynamically per tFunction, TransComponent #243

jlee0425 opened this issue Jan 26, 2023 · 0 comments

Comments

@jlee0425
Copy link

🚀 Feature Proposal

Improve parser.js to read ConditionalExpression to parse context.

Motivation

My project heavily uses i18n and sometimes context is necessary.
Problem is that each translation will have different values for the context and only a few of the keys actually use a context, and the current parser parses tOptions only when attr.value.type is one of: Literal, Identifier or ObjectExpression.

Example

What I would like to do is:

const { t } = useTranslation();

// component
<Trans 
  i18nKey="some.key"
  defaults="translated string"
  tOptions={{context: isTrue ? 'contextValue' : 'somethingelse'}}
/>

// better yet,
<Trans 
  i18nKey="some.key"
  defaults="translated string"
  context={isTrue ? 'contextValue' : 'somethingelse'}
/>

t('some.key2', 'translated string2', {context: isTrue ? 'withContext' : undefined});

// scanned
{
  "some.key": "translated string",
  "some.key_contextValue: "translated string",
  "some.key_somethingelse: "translated string",
  "some.key2": "translated string2",
  "some.key2_withContext": "translated string2",
}

Then, I would modify the translation with context accordingly.

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

1 participant