Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
storybook-addon-i18n
Advanced tools
Readme
Storybook I18n Addon can be used to change locale of the component inside the preview in Storybook.
This is how I18n addon looks like:
This addon is made library-agnostic, it does not depend on any exact i18n tool you use in your application.
It can take any custom locale context provider and pass any custom props.
It can be used even to test your components in ltr
and rtl
fashion.
npm i -D storybook-addon-i18n
Currently React is supported only. PR's are always welcome!
import "storybook-addon-i18n/register.js";
config.js
) add i18n
key to parameters:import { addParameters } from "@storybook/react";
addParameters({
i18n: {
provider: LionessProvider,
providerProps: {
messages
},
supportedLocales: ["en", "ru"],
providerLocaleKey: "locale"
}
});
config.js
)config.js
)import { addDecorator } from "@storybook/react";
import { withI18n } from "storybook-addon-i18n";
addDecorator(withI18n);
import { storiesOf } from "@storybook/react";
import { withI18n } from "storybook-addon-i18n";
storiesOf("Button", module).addDecorator(withI18n);
Library accepts following parameters, which are passed as storybook parameters under i18n
key:
provider
required - An internalization provider, which provides intl context to the appproviderProps
- All the props you need to pass to Provider, except localesupportedLocales
required - An array of locale keys that your application supportproviderLocaleKey
(locale
by default) - prop name of the locale used by the library to pass active locale to providerproviderDirectionKey
(direction
by default) - prop name of the direction key used by the libary to pass active direction (rtl
or ltr
) to providergetDirection
- function, which accepts locale as an argument and should return rtl
or ltr
. By default it is returning rtl
for he
and ar
localesIf you are using Material-UI, you need to test jss-rtl
in your storybook too. The problem is that you need to wrap your storybook to ThemeProvider
, which should recieve a theme
with correct direction.
You can check an integration example in my React boilerplate project.
To achive this task a common Provider should be created, which is used and in the Storybook and in the main application bundle. Here is an example:
export class MuiLocaleProvider extends React.PureComponent<WithLocale> {
public render() {
const { locale, direction } = this.props;
return (
<LionessProvider locale={locale} messages={messages}>
<MuiThemeProvider theme={createTheme(direction)}>
<JssProvider {...jss}>
<React.Fragment>
<CssBaseline />
{this.props.children}
</React.Fragment>
</JssProvider>
</MuiThemeProvider>
</LionessProvider>
);
}
}
Then this provider can be used in storybook config:
addParameters({
i18n: {
provider: MuiLocaleProvider,
providerProps: {
messages
},
supportedLocales
}
});
Currently, react-i18next doesn't support other props like locale
except i18n
. If you want to use i18n storybook addon, you need to wrap I18nProvider
with useEffect
hook.
export function I18nProviderWrapper({ children, i18n, locale }) {
React.useEffect(() => {
i18n.changeLanguage(locale);
}, [i18n, locale]);
return <I18nProvider i18n={i18n}>{children}</I18nProvider>;
}
This Provider wrapper should accept providerLocaleKey
as props in storybook decorator parameter. If the props value corresponding to providerLocaleKey
is changed, we can programmatically change the language.
Then this provider can be used in storybook config:
addParameters({
i18n: {
provider: I18nProviderWrapper,
providerProps: {
i18n
},
supportedLocales
}
});
FAQs
Storybook I18n Addon can be used to change locale of the component inside the preview in storybook
The npm package storybook-addon-i18n receives a total of 4,014 weekly downloads. As such, storybook-addon-i18n popularity was classified as popular.
We found that storybook-addon-i18n demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.