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

[Question] Interpolation: reference other keys? #872

Open
sheiman-pavlo opened this issue Jun 29, 2022 · 3 comments
Open

[Question] Interpolation: reference other keys? #872

sheiman-pavlo opened this issue Jun 29, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@sheiman-pavlo
Copy link

Is there a way we can reference other keys in dictionary files? An example below:

locales/en/common.json

{
  "one": "one",
  "you": "you're the $t(common:one)"
}

components/a-component.tsx

const { t } = useTranslation();
console.log(t`common:you`) // output: "you're the one"

Here is a similar feature in i18next, which I'm migrating from right now: https://www.i18next.com/translation-function/nesting

@aralroca
Copy link
Owner

Sorry but this feature is not implemented yet 😕 Please feel free to PR if you need it 🤗

How i18next manage this with other namespaces?

{
  "one": "one",
  "you": "you're the $t(anothernamespace:one)"
}

for now I see difficult to use the namespace here, you could only use namespaces that the page has loaded and not load them async. Maybe for a first iteration it would be fine without specifying the namespace, and just use them for the namespace itself.

{
  "one": "one",
  "you": "you're the $t(one)"
}

What do you think about this? Thanks!

@talyh
Copy link

talyh commented Jun 30, 2022

Echoing this request, as noted in #873

@talyh
Copy link

talyh commented Jul 2, 2022

@aralroca , about not referencing the namespace

For our use case, it'd likely yield little value to have composition without the namespace. Because the common / isolated words would likely exist in common or another generic file, while the longer terms leveraging those words are more page specific.

When you say:

I see difficult to use the namespace here, you could only use namespaces that the page has loaded and not load them async

As I don't fully understand the internals of the library, I'm not sure if the suggestion below helps as a potential path forward, but could this be solved with an assumption that the dev will load the namespace?

Taking the example here, say that we have this config

// common.json
{
  "one": "one",
}

// page.json
{
  // not going into whether {{}} or ${t()} for now, though I'd prefer for {{}} for readability
  "you": "you're the {{common:one}}"
}

// i18n.js
const i18n = {
  "*": ['common'],
  "/my-page": ["page.json"]
}

then the string translates to "you're the one" as expected.
If the dev didn't configure common so it'd be available for the page, it'd render as "you're the {{common:one}}"

Does this simplify the loading concern?

@aralroca aralroca added the enhancement New feature or request label Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants