-
Notifications
You must be signed in to change notification settings - Fork 2
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
SSR / Next.js example #6
Comments
Thanks for the idea! You're right, a Next.js example would be helpful. I'm not super familiar with nextjs but I'll give it a shot. In the meantime, if you get an example working can you make a pull request! :) Let me know if you have any other questions getting set up |
@willross Let me know if this helps https://github.com/hjylewis/babel-plugin-inline-i18n-messages/tree/master/examples/nextjs-example |
@hjylewis, thanks for the quick response! Your Next.js example has some issues from the Next.js project structure logic point of view, but they are easily fixed. The main problem is that I see that in your So far I'm unable to successfully reproduce this approach in Here's my repo, hope you'll have an opportunity to take a look: https://github.com/willross/nextjs-example. In it's current state it's working with the hardcoded locale, but doesn't allow changing languages. |
Apparently, currently you can't return an array of webpack configs in Next.js (vercel/next.js#1306). Do you think there's a way to achieve multi-locale functionality some other way? |
Hmm, that's what I was worried about.
PRs are welcome :)
Yeah, that's how the other examples work. You are basically building a version of your application for each locale.
You could set it up so that all locales are loaded. This would be suboptimal since it would increase bundle size but works with Next.js. It would also have the added benefit of allowing locale switching without a refresh To get that to work, your // getMessage
const fs = require("fs");
const path = require("path");
module.exports = function getMessage(key, locale, defaultMessage) {
const en = JSON.parse(fs.readFileSync(
path.resolve(process.cwd(), `./i18n/messages/en.json`)
));
const es = JSON.parse(fs.readFileSync(
path.resolve(process.cwd(), `./i18n/messages/es.json`)
));
return {
en: en[key] || defaultMessage,
es: es[key] || defaultMessage,
}
}; // addMessages
const messages = {
en: {},
es: {},
};
export function getMessages(locale) {
return messages[locale];
}
export function addMessages(idAndMessages) {
idAndMessages.forEach(([id, messageObject]) => {
Object.keys(messageObject).forEach(locale => {
messages[locale][id] = messageObject[locale];
});
});
} // App.js
...
<IntlProvider locale={locale} messages={getMessages(locale)}/>
... Unfortunately, since Nextjs doesn't make it easy to build multiple outputs there isn't a way to build per-locale bundles without ditching Nextjs. That said, you should be able to SSR per-locale bundles, just not with Nextjs. Does that help? Let me know if you have any questions |
Yup, this approach works! |
Hi there!
What you are doing is amazing and you are solving a very big problem for apps that have a lot of strings. I'm really glad this exists now!
However, from the current examples it's not immediately obvious how this would work with isomorphic apps, using Next.js for example.
So if you could provide some pointers or maybe create a Next.js example, you'd save someone's life :)
Thanks!
The text was updated successfully, but these errors were encountered: