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

Extract named parameters for some complex identifiers in t-macro #2033

Closed
tmcheung opened this issue Sep 11, 2024 · 2 comments
Closed

Extract named parameters for some complex identifiers in t-macro #2033

tmcheung opened this issue Sep 11, 2024 · 2 comments

Comments

@tmcheung
Copy link

tmcheung commented Sep 11, 2024

Problem

Simple identifier works great

When using string interpolation for message arguments like:

t(`${price} for ${numberOfDays} days`)

the parameter extracted is named:

i18n._(
  /*i18n*/
  {
    id: "+nhkwg",
    message: "{price} for {numberOfDays} days",
    values: {
      price: price,
      numberOfDays: numberOfDays,
    },
  }
);
// en/messages.po

msgid "{price} for {numberOfDays} days"

this is great as it provides context for translators about the available parameters.

All non-simple identifiers; not so great

However, it is only supported with simple identifiers, and anything else is replaced with positional parameters. The result is that identifiers/paths of nested references also get thrown out the window:

t(`${product.price} for ${product.numberOfDays} days`)
i18n._(
  /*i18n*/
  {
    id: "+nhkwgs",
    message: "{0} for {1} days",
    values: {
      0: product.price,
      1: product.numberOfDays,
    },
  }
);
// en/messages.po

msgid "{0} for {1} days"

Proposed solution

This behavior is defined here, but I would like it to not include nested paths as they are named. For nested paths it would be great if the leaf node or full path could be included so that it works like:

t(`${product.price} for ${product.numberOfDays} days`)
i18n._(
  /*i18n*/
  {
    id: "+nhkwgs",
    message: "{price} for {numberOfDays} days",
    values: {
      price: product.price,
      numberOfDays: product.numberOfDays,
    },
  }
);
// en/messages.po

msgid "{price} for {numberOfDays} days"
@andrii-bodnar
Copy link
Contributor

Thanks for the suggestion!

Actually, there is a new feature in the v5 pre-release that aims to provide additional context about placeholders in exactly the same case as you describe.

@timofei-iatsenko
Copy link
Collaborator

Here is a PR implementing this for more context:

#1965

BTW i still considering to implement optional full identifier as proposed in #1874

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

3 participants