goooseman / storybook-addon-i18n

Storybook I18n Addon can be used to change locale of the component inside the preview in storybook
34 stars 3 forks source link

Save last selected language and set a default language #13

Open carlitoplatanito opened 4 years ago

carlitoplatanito commented 4 years ago

Right now the language get's reset to the first locale in the supportedLocales array. Would like to request the possibility to set the default language, even if it's not the first one in the list.

Also if it could save the language I had selected for when I switch stories that would be great.

goooseman commented 4 years ago

@carlosl But why do you need to explicitly specify the default language, if you can just provide the default language as a first one in the array? What is your use case?

sogaani commented 4 years ago

I also want this feature though, I find a workaround like following.

export const supportedLocales = ['ja','en'];

export const I18nProvider: React.SFC<I18nProviderProps> = (
  props: I18nProviderProps
) => {
  const { children, i18n, locale } = props;
  useEffect(() => {
    i18n.changeLanguage(locale);
    // When locale is changed, make its position to supportedLocales[0]
    supportedLocales.sort((a, b) => {
      if (a === locale) return -1;
      if (b === locale) return 1;
      return 0;
    });
  }, [i18n, locale]);
  return <React.Suspense fallback={<Fragment />}>{children}</React.Suspense>;
};

addParameters({
  i18n: {
    provider: I18nProvider,
    supportedLocales,
    providerProps: {
      i18n,
    },
  },
});
goooseman commented 4 years ago

PR's are welcome! Also, this library is looking for maintainers!