synacor / preact-i18n

Simple localization for Preact.
BSD 3-Clause "New" or "Revised" License
206 stars 18 forks source link

Any way to get translated text as a string instead of a Preact component? #47

Closed elias6 closed 4 years ago

elias6 commented 4 years ago

I am using preact-i18n to render some components with translated text. But now, all of a sudden, we are using a 3rd-party API that requires a string to be passed to it. Is there any (easy) way to do this using preact-i18n?

I mentioned that here but I'm creating a new issue since I think this is not quite related. It would be so nice if the translate function in src/lib/translate.js were exposed.

donkeyDau commented 4 years ago

As mentioned in your linked issue you can use the useText hook. Drawback (compared to <Text>): you can use hooks only in the render method.

One-liner to translate a single id:

useText('Balances.tableHeadings.biddingZone').biddingZone
pl12133 commented 4 years ago

useText will be the easiest way to do it inside of a component, but if you are outside a component and need to translate something I think it's reasonable to use the translate function.

Can you test out https://github.com/synacor/preact-i18n/pull/48 to see if it works for you?

pl12133 commented 4 years ago

The translate function is now exported in preact-i18n@2.3.0-preactx.