i18next / react-i18next

Internationalization for react done right. Using the i18next i18n ecosystem.
https://react.i18next.com
MIT License
9.3k stars 1.03k forks source link

example of using react-i18next with storybook? #269

Closed anthonyettinger closed 7 years ago

anthonyettinger commented 7 years ago

Since I added react-i18next our storybook examples are completely broken.

How should we get i18next to work with storybook?

jamuhl commented 7 years ago

What issue you run into? Storybook is a regular webpack build.

Just use https://storybook.js.org/basics/writing-stories/#using-decorators to wrap the component into provider.

jamuhl commented 7 years ago

Made a sample: https://github.com/i18next/react-i18next/tree/master/example/storybook

jamuhl commented 7 years ago

closing for now...there is a working sample...if you could provide more info about your issue - please reopen this issue so we could get that to work properly for you...

valerioleo commented 5 years ago

working sample page is not available anymore. @jamuhl could you kindly check the url? I'm trying to solve the same issue 👍

jamuhl commented 5 years ago

it's moved to the v9 samples folder -> not had yet time to update it to v10

https://github.com/i18next/react-i18next/tree/master/example/v9.x.x/storybook

EdwardDrapkin commented 5 years ago

I haven't been able to get the <Trans> component to work at all.

I've distilled my story into this, which I realize is a mess, but it still won't render correctly:

import { storiesOf } from '@storybook/react';
import i18n from 'i18next';
import * as LanguageDetector from 'i18next-browser-languagedetector'
import * as localStorageBackend from 'i18next-localstorage-backend'
import * as React from 'react';
import { I18nextProvider, initReactI18next, Trans } from 'react-i18next';

const count = 10;
const name = 'bob';

const story = storiesOf('Translation Test', module);
story.add('Test', () => {
    i18n
        .use(initReactI18next)
        .use(LanguageDetector)
        .use(localStorageBackend)
        .init({
            fallbackLng: "en",
            interpolation: {
                escapeValue: false,
            },
            keySeparator: ".",

            react: {
                wait: true,
            },
        })
        .catch(e => window.console.error("App failed to start", e));

    return (
        <I18nextProvider i18n={i18n}>
            <Trans i18nKey="userMessagesUnread" count={count}>
                Hello <strong>{{ name }}</strong>, you have {{ count }} unread message.
            </Trans>
        </I18nextProvider>
    );
});

Objects are not valid as a React child (found: object with keys {count}).

jamuhl commented 5 years ago

You can't have Trans as a direct successor of I18nextProvider -> it returns an array of children to render per default -> wrap it into a div

EdwardDrapkin commented 5 years ago

Changed it to:

return (
        <I18nextProvider i18n={i18n}>
            <div>
                <Trans i18nKey="userMessagesUnread" count={count}>
                    Hello <strong>{{ name }}</strong>.
                </Trans>
            </div>
        </I18nextProvider>
    );

And still no dice.

This is my storybook config if it matters:

import { library } from "@fortawesome/fontawesome-svg-core";
import { faTimes } from "@fortawesome/free-solid-svg-icons";
import { faQuestionCircle } from "@fortawesome/pro-light-svg-icons";
import centered from "@storybook/addon-centered/dist/react";
import { withInfo } from "@storybook/addon-info";
import { addDecorator, configure } from "@storybook/react";
import * as React from "react";
import { createGlobalStyle, ThemeProvider } from "styled-components";
import { theme } from "../../modules/common/src/theme/Default";

const GlobalStyles = createGlobalStyle`
  @font-face {
      font-family: 'Rijsel', sans-serif;
      src: local('Rijsel'),
      url('../assets/fonts/Rijsel-Regular.otf') format('opentype');
  }
  @font-face {
      font-family: 'Rijsel-Light', sans-serif;
      src: local('Rijsel-Light'),
      url('../assets/fonts/Rijsel-Light.otf') format('opentype');
  }
  @font-face {
      font-family: 'Open Sans', sans-serif;
      @import url('https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i');
  }
  body {
      font-family: 'Open Sans', sans-serif;
  }
  *:focus {
    outline: none;
  }
`;

function withGlobalStyles(storyFn) {
    return (
        <>
            <GlobalStyles/>
            <ThemeProvider theme={theme}>
                {storyFn()}
            </ThemeProvider>
        </>
    );
}

function runStories() {

    // add font awesome icons
    library.add(faTimes, faQuestionCircle);
    addDecorator(centered);
    addDecorator(withGlobalStyles);
    addDecorator(withInfo({
        styles: {
            infoBody: {
                fontFamily: "courier",
                fontSize: "12px",
            },
            infoTable: {
                display: "none",
            },
        },
    }));

    const req = require.context("../../modules/story/src", true, /\.story\.tsx$/);
    req.keys().forEach(f => req(f));
}

configure(runStories, module);

Thanks for your help.

jamuhl commented 5 years ago

@EdwardDrapkin not sure by just reading your snippets. But try to do it like https://github.com/i18next/react-i18next/blob/master/example/v9.x.x/storybook/stories/withI18nextProvider.js (Having i18n.js outside the story, the Provider as Decorator)

zhhuan commented 3 years ago

@EdwardDrapkin have you solved this issue? I met the same issue for Trans component

Gabsii commented 3 years ago

Is there an updated version for Storybook 6?

adrai commented 3 years ago

Is there an updated version for Storybook 6?

Updated last month: https://github.com/i18next/react-i18next/tree/master/example/storybook

dcarias0388 commented 2 years ago

Hola, tengo un componente que en el que se realiza la siguiente importacion import { i18n, I18nContext, useTranslation } from 'next-i18next'; . Cuando le estoy haciendo el stories a dicho componente me muestra el siguiente error: next_i18next__WEBPACK_IMPORTED_MODULE_4__.i18n is null . Que me pudiera estar faltando por agregar en la configuracion?

adrai commented 2 years ago

Hola, tengo un componente que en el que se realiza la siguiente importacion import { i18n, I18nContext, useTranslation } from 'next-i18next'; . Cuando le estoy haciendo el stories a dicho componente me muestra el siguiente error: next_i18next__WEBPACK_IMPORTED_MODULE_4__.i18n is null . Que me pudiera estar faltando por agregar en la configuracion?

you may have a look at: https://github.com/i18next/next-i18next/issues/1843 or as at Stackoverflow

dcarias0388 commented 2 years ago

[>](FireShot Capture 067 - Design System _ Molecules _ User Lang Options - Default ⋅ Storybook_ - localhost) > Hola, tengo un componente que en el que se realiza la siguiente importacion import { i18n, I18nContext, useTranslation } from 'next-i18next'; . Cuando le estoy haciendo el stories a dicho componente me muestra el siguiente error: next_i18next__WEBPACK_IMPORTED_MODULE_4__.i18n is null . Que me pudiera estar faltando por agregar en la configuracion?

puede echar un vistazo a: i18next/next-i18next#1843 o como en Stackoverflow

Si a mi las traducciones me funcionan bien, solo ahora me topo con un componente que dentro realiza la siguiente importacion import { i18n, I18nContext, useTranslation } from 'next-i18next', y me devuelve el error next_i18next__WEBPACK_IMPORTED_MODULE_4__.i18n is null.