Why to use WithI18n instead of i18n object directly
See original GitHub issueHi, I encounter a problem when I use WithI18n, I got the following messages:
./src/App1/routes/Home/components/HomeView.js
Module build failed: SyntaxError: Unexpected token, expected ; (6:45)
4 | import './HomeView.scss'
5 |
> 6 | export const HomeView = WithI18n()({ i18n }) => {
| ^
7 | return (
8 | <div>
9 | <h3><Trans>Welcome App 1!</Trans></h3>
@ ./src/App1/routes/Home/index.js 1:0-45
@ ./src/App1/routes/index.js
@ ./src/App1/main.js
The code is
import React from 'react'
import { WithI18n, Trans } from 'lingui-react'
export const HomeView = WithI18n()({ i18n }) => {
return (
<div>
<h3><Trans>Welcome App 1!</Trans></h3>
<img
src="/static/logos/logo_client.png"
alt={i18n.t`Logo client`}
/>
</div>
)
}
export default HomeView
My configuration is:
├─┬ babel-preset-lingui-react@1.0.1
│ ├── babel-plugin-lingui-transform-js@1.0.1
│ └── babel-plugin-lingui-transform-react@1.0.1
├─┬ lingui-cli@1.0.4
│ ├── babel-plugin-lingui-extract-messages@1.0.2
│ ├─┬ lingui-conf@0.8.1
├─┬ lingui-i18n@1.1.0
│ └── lingui-formats@1.0.1
├── lingui-react@1.0.2
And I use Redux with this starter kit
So I tried import i18n from 'lingui-i18n'
and everything works.
So why one should use WithI18n ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Internationalize your Next application with i18n and TypeScript
To type keys, it is recommended to prepare new HOC and hook and use them instead of using i18next modules directly. Create your...
Read more >useTranslation (hook) - react-i18next documentation
When to use? Use the useTranslation hook inside your functional components to access the translation function or i18n instance.
Read more >The Ultimate Guide to Node.js Internationalization (I18n)
Learn how to set up your Node.js i18n project and organize your translations to make your app ready for a global user base....
Read more >lingui/core - The core i18n functionality - LinguiJS
In most cases you can use the global i18n object exported from @lingui/core directly. However, if you do need to setup your own...
Read more >Localization in Node.js and Express.js with i18n examples
Use pluralization formats, where forms of words change depends on object counts. Some of them automatically translate dates, weekdays, months, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
On the note of the subject, I made a discovery of how to do that. It’s a MobX based solution which feels the easier for me, but it can be surely tackled with various other approaches. They key point here is to have a i18nProvider to re-render everything when language changes.
Now you can use
import { i18n } from 'fileabove.tsx'
anywhere in your app. No need to usewithi18n
anymore 🎉 When you want to change the language, just callchangeLanguage
. Similar functionality could be done if you are loading & merging catalogs based on code splitting. Then you also need to re-render whole app.This whole solution is obviously against some ground rules of not using global stuff, but honestly, I don’t think it matters in this case. Only, hurdle might be with unit testing where the state (
i18n
) would be shared, but as long as you don’t want to test localization, it should be fine 😃This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.