storybook-addon-i18n icon indicating copy to clipboard operation
storybook-addon-i18n copied to clipboard

Save last selected language and set a default language

Open carlitoplatanito opened this issue 5 years ago • 3 comments

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.

carlitoplatanito avatar Feb 20 '20 16:02 carlitoplatanito

@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?

goooseman avatar Feb 23 '20 14:02 goooseman

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

sogaani avatar Jun 19 '20 12:06 sogaani

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

goooseman avatar Sep 22 '20 04:09 goooseman