-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
i18next-icu interoperability with Trans #439
Comments
correct You will need to pass options to t function via prop tOptions for ICU like: https://github.com/i18next/react-i18next/blob/master/example/react_icu_withHOC/src/App.js#L29 |
Yeah, I saw that last example, but in our case this doesn't work with our solution to key-based fallbacks as we use the Trans children as the key. Our codebase would contain snippets such as:
and there's simply no such way to do that with Trans, as
results in the incorrect key Furthermore,
simply results in invalid JSX. For me this is just seems to be an incompatability between ICU + Trans + key-based fallback, and I'd like to find a solution (at the very least for basic interpolation). |
hm...might need some different key generation. Would need to test more...but currently lack the time. What if you escape the curly brackets?? facebook/react#1545 |
The issue is the opposite - I need "real" double curbly brackets for react-i18next to pick up the variable name ( Lines 34 to 41 in 8a18a3b
For react-icu one would need a way to override this to use single brackets instead. |
so you want to have jsx what i would do is: jsx: i see no other way. Doing a transform of double curly won't work as you could not write complex plural statements that way...only other option would be the creation of some babel-transform which takes |
Awesome would be to create babel-macro a https://github.com/kentcdodds/babel-plugin-macros/ doing that conversion but would take some time or better someone with skills writing babel-plugins |
@beheh sorry for the late update...was blocked with other topics. with v7.7.0 you now can: <Trans defaults="There are {numPersons, plural, =0 {no persons} =1 {one person} other {# persons}}" values={{ numPersons: 10 }} />
// -> There are 10 persons or interpolate: <Trans defaults="some {thing}" values={{ thing: 'awesomeness' }} />
// -> some awesomeness or components: <Trans defaults="hello <0>{{what}}</0>" values={{ what: 'world'}} components={[<strong>univers</strong>]}> />
// -> hello <strong>world</strong> from here it should be rather easy to create a babel makro converting <TransMakro values={{ thing: 'awesomeness' }}>some {thing}</TransMakro> to <Trans defaults="some {thing}" values={{ thing: 'awesomeness' }} /> |
@beheh I've been using I guess that unless there is some babel transform, |
This is great, thanks! I think we might just be skipping the babel plugin here and using the I did notice that I need to fill the Great work! |
Yes...those components needs childrens right now - might optimize that in future so another issue for that might be a good idea. Wound't hurt as the Trans component used this way works rather different from the way having the content in the childrens (jsx tree) as the indexes for those are different. But guess thats ok. |
@beheh @kachkaev just made a non-so-quick try and created a babel-plugin-macros (https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md) for using Trans and ICU: Just a quick and dirty implementation for a macro: https://github.com/i18next/react-i18next/blob/master/example/react_icu_withHOC/src/Trans.macro.js let's you basically write something like: <Trans>Trainers: <strong>{ trainersCount, number }</strong>!</Trans>
<Select
switch={gender}
male={<Trans><strong>He</strong> avoids bugs.</Trans>}
female={<Trans><strong>She</strong> avoids bugs.</Trans>}
other={<Trans><strong>They</strong> avoid bugs.</Trans>}
/>
<Plural
count={itemsCount1}
$0="There is no item."
one="There is # item."
other="There are # items."
/> Before cleaning that up feedback would be very welcome... |
We have a project where we use react-i18next, i18next-icu and key-based fallbacks. This means we extract keys from code as
<Trans>Hello world!</Trans>
to a language file in the following fashion:{"Hello World!": "Hallo Welt!"}
.This mostly worked fine until we introduced i18next-icu. While usage of
t
works as expected (i.e.t("You have {msgCount, plural, one {# message} other {# messages}}")
), I'm having issues with getting the Trans to work. Specifically<Trans>some {{data}}</Trans>
leads to a the translateable stringsome {{data}}
, which is invalid in ICU (throws a syntax error due to the unexpected second opening brace).I tried running with
<Trans>some {data}</Trans>
, but obviously the variable resolution fails here without the object syntax, so I think I'll definitely need to use the double brace syntax. As I understand this is just a quirk of how react-i18next works, so I'm interested in getting thThis leaves me with three places this could be fixed:
<Trans>some {{stuff}}</Trans>
should resolve to/lookupsome {stuff}
. If this would be accepted I'd be happy to work on a PR.t(
some {{stuff}})
, it would rewrite it internally tosome {stuff}
before running the ICU parser). In that case I would close the issue here and work with i18next-icu.Thoughts?
The text was updated successfully, but these errors were encountered: