question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lingui handling only strings in variables

See original GitHub issue

Hello Lingui team.

This is more of a feature request, to see what’s your view on this.

So, currently lingui only handles strings as variables, so I cannot do something like this:

const savings = <NumberFormat value={123} />;
return <Trans>With our company you save {savings}!!</Trans>

this will result in: With our company you save [object Object]

You may wonder why this is important for me. Well, we have a fairly big application which shows all kind of numbers and amounts in different places, thus we use redux selectors for a lot of them. So, in order to reduce duplicated code the values are returned from selectors as a <NumberFormat> component. And then it is used in some translatable message like “With us you save {savings}”. This is also nice for translators because they see the variable name and understand the context of given sentence.

The workaround that I have for this issue is this component:

const Tag = (props) => (props.content);
export default Tag;

and use it like this:

const savings = <NumberFormat value={123} />;
return <Trans>With our company you save <Tag content={savings} />!!</Trans>

but it’s less optimal because translators then get With our company you save <0>!!

Is there any plan to add support to React components in variables? Looking in the code there’s few places where array are joined together into one string, than this string is parsed in other place, so that’s not one line change to make it work. But shouldn’t be too hard either.

I could try doing this, but I need your opinion on this first.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tricoder42commented, Jul 23, 2018

Fixed in latest release v2.3.0.

0reactions
haruelrovixcommented, Jan 25, 2019

Hi @tricoder42 @machour @ktunkiewicz ,

Is it possible to implement this but use a combination of i18n and t macro instead of Trans component?

e.g.

i18n._(t`Hi ${<strong>user.name</strong>}`)

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common i18n patterns in React — LinguiJS documentation
<Trans> handles translations of messages including variables and other React ... Or to render the message descriptor as a string-only translation, just pass ......
Read more >
Guide to translate your React and JS applications using Lingui
Follow our step-by-step guide to install, configure and use our Lingui integration to help you localize and translate your React and JS applications....
Read more >
lingui/js-lingui - Gitter
Hi! At the moment I'm using react-intl , but I'm looking into migrating to lingui . Anyway, my question is somewhat unrelated to...
Read more >
lingui-react - npm
lingui -react · ⚠️ Only simple variables are supported inside Trans component. · ⚠️ Watch the empty braces after WithI18n . WithI18n expects ......
Read more >
Handling text — strings in JavaScript - Learn web development
Just like we did with numbers, we are declaring a variable, initializing it with a string value, and then returning the value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found