next.js 下一个-翻译成故事书

57hvy0tb  于 2023-06-05  发布在  其他
关注(0)|答案(2)|浏览(822)

我正在寻找通过下一个翻译加载静态文本到故事书的方法。
我的代码看起来像这样,但是它加载了我的语言环境文件,但是没有正确地编写它们。

这是故事书预览,js:

import '../src/styles/global/global.scss';

import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import theme from '../src/utils/theme';
import I18nProvider from 'next-translate/I18nProvider';

import commonCS from '../locales/cs/common.json';

export const decorators = [(Story) => themeDecorator(Story)];

const themeDecorator = (Story) => {
    console.log(commonCS.homepage_title);
    return (
        <ThemeProvider theme={theme}>
            <CssBaseline />
            <I18nProvider lang={'cs-CS'} namespaces={{ commonCS }}>
                <Story />
            </I18nProvider>
        </ThemeProvider>
    );
};

export const parameters = {
    actions: { argTypesRegex: '^on[A-Z].*' },
    controls: { expanded: true },
};

这是我的故事书

import React from 'react';
import HeaderContact from './HeaderContact';
import I18nProvider from 'next-translate/I18nProvider';
import useTranslation from 'next-translate/useTranslation';
import commonCS from '../../../locales/cs/common.json';

export default {
    title: 'HeaderContact',
    component: HeaderContact,
};

export const Basic = () => {
    const { t } = useTranslation('common');
    return (
        <HeaderContact
            link="mailto:info@numisdeal.com"
            text={t('homepage_title')}
        />
    );
};

我本地文件common.json:

{
    "homepage_title": "Blog in Next.js",
    "homepage_description": "This example shows a multilingual blog built in Next.js with next-translate"
}

还有我的翻译配置i18n.json

{
    "locales": ["cs", "en", "de"],
    "defaultLocale": "cs",
    "pages": {
        "*": ["common"]
    }
}

我很乐意得到一些帮助。谢谢!罗曼

mzmfm0qo

mzmfm0qo1#

这是解决方案。

preview.js

import '../src/styles/global/global.scss';

import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import theme from '../src/utils/theme';
import I18nProvider from 'next-translate/I18nProvider';

import commonCS from '../locales/cs/common.json';

export const decorators = [(Story) => themeDecorator(Story)];

const themeDecorator = (Story) => {
    console.log(commonCS.homepage_title);
    return (
        <ThemeProvider theme={theme}>
            <CssBaseline />
            <I18nProvider lang={'cs'} namespaces={{ common: commonCS }}>
                <Story />
            </I18nProvider>
        </ThemeProvider>
    );
};

export const parameters = {
    actions: { argTypesRegex: '^on[A-Z].*' },
    controls: { expanded: true },
};

故事:

import React from 'react';

import HeaderContact from './HeaderContact';

export default {
    title: 'HeaderContact',
    component: HeaderContact,
};

export const Basic = () => {
    return <HeaderContact link="mailto:info@numisdeal.com" />;
};

组件:

import React from 'react';
import AlternateEmailIcon from '@material-ui/icons/AlternateEmail';
import useTranslation from 'next-translate/useTranslation';

import styles from './HeaderContact.module.scss';

export interface IHeaderContact {
    link: string;
    text?: string;
}

export default function HeaderContact(props: IHeaderContact) {
    const { link, text } = props;
    const { t } = useTranslation('common');
    const preklad = t('homepage_title');

    return (
        <a href={link} className={styles.headerLink}>
            <AlternateEmailIcon fontSize="small" />
            <span>
                {/* {text} */}
                {preklad}
            </span>
        </a>
    );
}
fkaflof6

fkaflof62#

我把它添加到我的storybook preview.js配置中,对我来说效果很好。
首先导入commons和provider

import I18nProvider from 'next-translate/I18nProvider';
import commonEN from '../locales/en/common.json';
import commonES from '../locales/es/common.json';

在此之后,添加装饰器以基于导航器语言更改已解析的公共

const themeDecorator = (Story) => {
  const userLanguage = window.navigator.language.startsWith('en') ? 'en' : 'es';

  const commons = userLanguage === 'en' ? commonEN : commonES;

  return (
    <I18nProvider lang={userLanguage} namespaces={{ common: commons }}>
      <Story />
    </I18nProvider>
  );
};

export const decorators = [themeDecorator];

这对我很有效。我检查了两种语言是否都能工作,创建了两个Chrome配置文件,一个默认语言是西班牙语,另一个是英语

相关问题