npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

storybook-addon-i18n

v5.1.13

Published

Storybook I18n Addon can be used to change locale of the component inside the preview in storybook

Downloads

18,738

Readme

Storybook I18n

Storybook I18n Addon can be used to change locale of the component inside the preview in Storybook.

This is how I18n addon looks like:

Storybook I18n Demo

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.

Installation

npm i -D storybook-addon-i18n

Simple Usage

Currently React is supported only. PR's are always welcome!

React

  1. Create a file called addons.js in your Storybook config, if there is no any and append following line:
import "storybook-addon-i18n/register.js";
  1. Then in your story's config or in a global config for the project (config.js) add i18n key to parameters:
import { addParameters } from "@storybook/react";

addParameters({
  i18n: {
    provider: LionessProvider,
    providerProps: {
      messages
    },
    supportedLocales: ["en", "ru"],
    providerLocaleKey: "locale"
  }
});
  1. Finally, Add decorator in your story's config or in a global config for the project (config.js)
  • global config (config.js)
import { addDecorator } from "@storybook/react";
import { withI18n } from "storybook-addon-i18n";

addDecorator(withI18n);
  • story's config
import { storiesOf } from "@storybook/react";
import { withI18n } from "storybook-addon-i18n";

storiesOf("Button", module).addDecorator(withI18n);

API

Library accepts following parameters, which are passed as storybook parameters under i18n key:

  • provider required - An internalization provider, which provides intl context to the app
  • providerProps - All the props you need to pass to Provider, except locale
  • supportedLocales required - An array of locale keys that your application support
  • providerLocaleKey (locale by default) - prop name of the locale used by the library to pass active locale to provider
  • providerDirectionKey (direction by default) - prop name of the direction key used by the libary to pass active direction (rtl or ltr) to provider
  • getDirection - function, which accepts locale as an argument and should return rtl or ltr. By default it is returning rtl for he and ar locales

Complex usage

Material-UI

If 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
  }
});

react-i18next

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
  }
});