next-i18next is built to work server-side as well as client-side. Storybook doesn't support server-side rendering so there's nowhere to add the next-i18next middleware. The good news is that means we don't have to support server-side rendering and can just use the underlying react-i18next and i18next-instance.
We're going to add a decorator
which will allow us wrap all stories in the <I18nextProvider>
.
The decorator will be added to .storybook/preview.js
so you need to create one if you haven't already.
import { I18nextProvider } from 'react-i18next';
import i18n from './i18n';
addDecorator(storyFn => (
<I18nextProvider i18n={i18n}>{storyFn()}</I18nextProvider>
));
We can't use the same i18n config that we use in next-i18next. We need to use a react-i18next instance.
I recommend creating the file at .storybook/i18n.js
since it's just for Storybook config. The setup can be as simple
as this:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
i18n.use(initReactI18next).init({
fallbackLng: 'en',
debug: true
});
export default i18n;
The last step is to tell Storybook that it should serve the locales dir as a static path.
We achieve this with the -s
CLI flag.
{
"scripts": {
"start-storybook": "start-storybook -s ./public"
}
}
@hxxiaolong That's likely related to the new locale routing functionality released in v10 of Next.js. I don't know much about that. Perhaps you could ask in their discussions. It's probably just a setting that needs to be mocked.