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
How to implement i18n React example on SSR? #715
Comments
On serverside most implementations will use a proper setup using i18next-express-middleware configured to load all languages, namespaces upfront -> therefore not run into the useTranslation, withTranslation run into throwing a Promise to trigger a Suspense |
Thanks @jamuhl , I'm trying i18next-express-middleware out on my project, however I'm getting the following issue: I think perhaps it is because of the latest version I installed using npm ("i18next-express-middleware": "^1.7.1",), that is different from the one used on the examples of i18next-express-middleware ("i18next-express-middleware": "1.0.5", ); |
you're using this on client?!? if i see the last bit of the printscreen right - "webpack plugin" the express-middleware is a serverside node.js package... ---> consider using: https://github.com/isaachinman/next-i18next |
Ok, I'll give it a try, thanks @jamuhl ! :) Since I wasn't involved too deep on the server-side rendering process we're using now, I think probably should've gone for the client side package since the beggining |
I also don't understand how this could work? |
@nicksrandall Suspense will be only triggered when you render the serverside JSX tree and not all translation files (namespaces) are loaded. If you run into that situation on the server - i would suggest preloading all translations on serverside using Preloading all translations on serverside is not a bad thing...we do not create a empty i18n instance on request but a clone sharing the translations with the main instance -> so no memory pressure. |
Do you have any guidance on how to implement SSR without using express? I used to be able to use |
@odensc there now is a suspense free mode not yet had time to document that. sorry |
Hi @odensc! Currently having same thing on my mind. Did you find any solutions? |
react: {
useSuspense: false
}
const i18n = baseI18n.cloneInstance();
i18n.language = getPreferredLanguage(req);
<I18nextProvider i18n={i18n}>
[...]
</I18nextProvider>
const i18nState = await getInitialProps(i18n);
[...]
<script
defer
dangerouslySetInnerHTML={{
__html: `window.__I18N_STATE__=${serialize(i18nState)}`
}}
/>
I had some issues with |
Nice! I'll have a look along that kind of path! :) Seems doable! Thank you very much! |
Thanks for the code @odensc . I found Although I am struggling getting the initial data out of my store. Maybe it's not initialised correctly, or maybe I can't get it out. Which version of i18next are you using? As for me I even tried simply copying the code here, and this didn't work either. Although I think there is something wrong with my config: i18next
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// use node-fs instead of xhr backend
.use(nodeFsBackend)
.init({
keySeparator: '__KEY__',
defaultNS: 'translation',
interpolation: {
// react is already safe from xss
escapeValue: false,
},
backend: {
loadPath: path.resolve(__dirname, `/locales/{{lng}}/{{ns}}`),
parse: (data: any) => data,
},
react: {
useSuspense: false,
},
}) Then in my controller I do something like this: const lang = detectLanguageFromUser(req)
const i18n = i18next.cloneInstance()
i18n.language = lang
await i18n.loadLanguages(lang) And I am stuck here :D |
Hi @bitttttten, I'm not quite sure how to solve your issue. I'm using |
It's just the types that are out of date.. if I use your code with Thanks! |
I just tell about my case:
Just load all i18n resource on server =) |
Question about react-i18next
Hi. The following React i18n example:
https://github.com/i18next/react-i18next/tree/master/example/react
works well by itself, I mean when ran locally using the given steps, but since Suspense is not yet supported by ReactDOMServer on server-side-rendering I was wondering if anyone had any ideas on how this same example would look like if is intended to be SSR, since it is failing on that condition?
Source: https://reactjs.org/docs/code-splitting.html
The text was updated successfully, but these errors were encountered: