react项目实现中英文国际化切换

antd组件的国际化

import zhCN from 'antd/locale/zh_CN';
// for date-picker i18n
import 'dayjs/locale/zh-cn';

return (
  <ConfigProvider locale={zhCN}>
   <Pagination defaultCurrent={1} total={50} showSizeChanger />
  </ConfigProvider>
);

自己写的内容的国际化(利用react-intl库)

pnpm install react-intl
<IntlProvider locale={locale} messages={locales[locale]}>
    <FormattedMessage id="welcome" />
 </IntlProvider>

自己写的内容需要IntlProvider包裹,locale是语言就是自己定义的,但是要和自己写的locales文件中一样,message就是匹配自己定义的locales中的内容了;用FormattedMessage 中的id来匹配对应的内容

整体代码
App.tsx

import  { useState } from 'react';
import enUS from 'antd/locale/en_US';
import zhCN from 'antd/locale/zh_CN';
import { Button, ConfigProvider, Pagination } from 'antd';
import { FormattedMessage, IntlProvider } from 'react-intl';
import locales from '@/locales/locales';

const App = () => {
  const [locale, setLocale] = useState('en'); // 默认语言为英文

  const switchToEnglish = () => {
    setLocale('en');
  };

  const switchToChinese = () => {
    setLocale('zh');
  };

  return (
    <>
      <div>
        <Button onClick={switchToEnglish}>English</Button>
        <Button onClick={switchToChinese}>中文</Button>
      </div>
      <ConfigProvider locale={locale === 'en' ? enUS : zhCN}>
        <IntlProvider locale={locale} messages={locales[locale]}>
          {/* 其他应用内容 */}
          <FormattedMessage id="welcome" />
          <Pagination defaultCurrent={1} total={50} showSizeChanger />
        </IntlProvider>
      </ConfigProvider>
    </>
  );
};

export default App;

src->locales–>locales.ts

const locales: { [key: string]: { [key: string]: string } } = {
    en: {
      welcome: "Welcome",
      greeting: "Hello, how are you?"
    },
    zh: {
      welcome: "欢迎",
      greeting: "你好,你好吗?"
    }
  };
  
  export default locales;

运行效果
英文
在这里插入图片描述
中文
在这里插入图片描述

实现中英文切换,最常见的做法是使用国际化(i18n)库,例如 react-intl。以下是基本的实现步骤: 1. 安装 react-intl 库:`npm install react-intl` 2. 创建一个包含中英文文本的语言文件,例如 `src/lang.json`: ``` { "hello": { "en": "Hello", "zh": "你好" }, "world": { "en": "World", "zh": "世界" } } ``` 3. 在组件中引入 `FormattedMessage` 组件,并使用 `id` 属性指定需要显示的文本对应的键值: ```jsx import { FormattedMessage } from 'react-intl'; import lang from './lang.json'; function Greeting() { return ( <div> <FormattedMessage id="hello" defaultMessage="Hello" />{' '} <FormattedMessage id="world" defaultMessage="World" /> </div> ); } ``` 4. 在顶层组件中使用 `IntlProvider` 组件,指定当前语言和语言文件: ```jsx import { IntlProvider } from 'react-intl'; import lang from './lang.json'; function App() { const locale = 'zh'; // or 'en' return ( <IntlProvider locale={locale} messages={lang[locale]}> <Greeting /> </IntlProvider> ); } ``` 5. 在需要切换语言的地方,更新 `locale` 和 `messages` 属性即可。 ```jsx function LanguageSwitcher() { const [locale, setLocale] = useState('zh'); function handleSwitch() { setLocale(locale === 'zh' ? 'en' : 'zh'); } return ( <div> <button onClick={handleSwitch}>Switch Language</button> <IntlProvider locale={locale} messages={lang[locale]}> <Greeting /> </IntlProvider> </div> ); } ``` 以上就是使用 react-intl 实现中英文切换的基本步骤。当然,具体实现还可以根据项目需求进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值