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

@wtg/storybook-i18n

v1.1.4

Published

Tool to set the locale in Storybook for i18n. A fork while a PR pends.

Downloads

346

Readme

Storybook-i18n

A library for best-practice i18n addons in Storybook:

  • Easy-to-use locale configuration
  • Simple drop-down menu
  • URL-linkable state for sharing

Requires storybook >=6.5.x

Addon authors

As an addon author, you can use this library by adding it as a dependency and adding the following to your /preset.js file:

function config(entry = []) {
    return [
        ...entry,
        require.resolve('storybook-i18n/preview'), // <-- library's preview preset
        require.resolve('./dist/esm/preset/preview'), // <-- your addon's preview preset (if present)
    ];
}

function managerEntries(entry = []) {
    return [
        ...entry,
        require.resolve('storybook-i18n/manager'),
        require.resolve('./dist/esm/preset/manager'), // <-- your addon's manager (if present)
    ];
}

module.exports = {config, managerEntries};

The currently selected locale is available in the locale global, so you can access it in a decorator using the following snippet:

import { MyProvider } from 'your-i18n-library';
import { useGlobals } from '@storybook/client-api';

const myDecorator = (story, context) => {
  const [{locale}] = useGlobals();
  
  return <MyProvider locale={locale}>;
}

End users

End users configure the locales and locale parameters in .storybook/preview.js.

locales is an object where the keys are the "ids" of the locale/language and the values are the plain text name of that locale you want to use. This is what will appear in the dropdown in the toolbar.

export const parameters = {
  locale: "en",
  locales: {
    en: "English",
    fr: "Français",
    ja: "日本語",
  },
};

Users can also use full locale strings.

export const parameters = {
  locale: "en_US",
  locales: {
    en_US: "English (US)",
    en_GB: "English (GB)",
    fr_FR: "Français",
    ja_JP: "日本語",
  },
};

The locales object can also have values as an object with keys of title, left, or right.

This is useful if you want to include an emoji flag or some other string to the left or right side.

For example:

export const parameters = {
    locale: "en",
    locales: {
        en: {title: "English", left: '🇺🇸'},
        fr: {title: "Français", left: '🇫🇷'},
        ja: {title: "日本語", left: '🇯🇵'},
    },
};

Or something like this:

export const parameters = {
  locale: "en_US",
  locales: {
    en_US: {title: "English", right: 'US'},
    en_GB: {title: "English", right: 'GB'},
    fr_FR: {title: "Français", right: 'FR'},
    ja_JP: {title: "日本語", right: 'JP'},
  },
};

When the locale has been changed, an event is emitted on the addons-channel.

You can subscribe to this event in your preview.js, to configure global environment settings yourself, related to your i18n-config.

The event is emmited with the selected locale as a parameter.

Your implementation could look like this:

import {addons} from '@storybook/addons'

addons.getChannel().on('LOCALE_CHANGED', (newLocale) => {
   changeMyI18nConfig(newLocale)
});

Addons should instruct them to use whichever format your i18n implementation expects.