storybook-next-intl
Version:
Add next-intl support to Storybook
42 lines (38 loc) • 1.14 kB
JavaScript
import i18n from 'storybook-i18n/preview';
import React from 'react';
import { useGlobals } from 'storybook/preview-api';
import { NextIntlClientProvider } from 'next-intl';
// src/preview.ts
var withNextIntl = (StoryFn, context) => {
const [{ locale }] = useGlobals();
const {
parameters: { nextIntl }
} = context;
if (nextIntl === void 0) {
console.error(
`The 'nextIntl' parameter is missing in 'parameters' configuration of preview.js. Check the README for instructions.`
);
return StoryFn(context);
}
const { defaultLocale, messagesByLocale } = nextIntl || {};
const currentLocale = locale || defaultLocale;
const currentMessages = messagesByLocale[currentLocale] || {};
return /* @__PURE__ */ React.createElement(
NextIntlClientProvider,
{
...nextIntl,
locale: currentLocale,
messages: currentMessages
},
StoryFn(context)
);
};
// src/preview.ts
var i18nDecorators = i18n.decorators || [];
var preview = {
...i18n,
// @ts-ignore
decorators: [...i18nDecorators, withNextIntl]
};
var preview_default = preview;
export { preview_default as default };