使用Tailwind CSS和i18n的react实践

首先在 src 下设置 i18n.js 文件

TypeScript 复制代码
// src/i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import en from './locales/en/public';
import zh from './locales/zh/public';

i18n
  .use(initReactI18next)  
  .init({
    resources: {
      en: { translation: en },//翻译文本
      zh: { translation: zh },
    },
    lng: 'zh',  
    fallbackLng: 'en', // When no lang
    interpolation: {
      escapeValue: false, // React avoid XSS
    },
  });

export default i18n;

然后再主文件引入文件

TypeScript 复制代码
import RouterGuard from './common/components/router-wrapper/RouterGuard'
import { Router } from './routes/router/RouterRender'
import routesConfig from './routes'
import { BrowserRouter } from 'react-router-dom'
import 'antd/dist/reset.css'
import './index.css'
import './i18n' // 引入
function App() {
  return (
    <BrowserRouter>
      <RouterGuard>
        <Router routes={routesConfig} />
      </RouterGuard>
    </BrowserRouter>
  )
}
export default App

最后直接使用就行

TypeScript 复制代码
import { message } from 'antd';
import './index.scss'
import { useTranslation } from 'react-i18next';
const DataCard = () => {
  const { t, i18n } = useTranslation();
  const toggleLanguage = () => {
    const newLang = i18n.language === 'zh' ? 'en' : 'zh';
    i18n.changeLanguage(newLang);
  };
   return (
    <div>
        <div style={{ padding: 20 }}>
          <h1 className="text-3xl font-bold mb-4">{t('currentName')}</h1>
          <button className="bg-blue-400 hover:bg-blue-200 text-white font-bold py-2 px-3 rounded transition duration-100" 
          onClick={toggleLanguage}>
            {t('check')}
          </button>
          <div className="buttons mt-6">
            <button className="bg-red-400 hover:bg-red-200 text-white font-bold py-2 px-4 rounded transition duration-100 mr-2" 
            onClick={() => message.success(t('successfullyDeleted'))}>
              {t('delete')}
            </button >
            <button className="bg-yellow-400 hover:bg-yellow-200 text-white font-bold py-2 px-4 rounded transition duration-100 mr-2" 
            onClick={() => message.success(t('successfulOperation'))}>
              {t('edit')}
            </button>
            <button className="bg-green-400 hover:bg-green-200 text-white font-bold py-2 px-4 rounded transition duration-100"  onClick={() => message.success(t('successfulOperation'))}>
              {t('create')}
            </button>
          </div>
        </div>
      </div>
    )
相关推荐
懂懂tty1 天前
React状态更新流程
前端·react.js
小码哥_常1 天前
告别繁琐!手把手教你封装超实用Android原生Adapter基类
前端
skywalk81631 天前
pytest测试的时候这是什么意思?Migrating <class ‘kotti.resources.File‘>
前端·python
一只蝉nahc1 天前
vue使用iframe内嵌unity模型,并且向模型传递信息,接受信息
前端·vue.js·unity
子兮曰1 天前
Bun v1.3.12 深度解析:新特性、性能优化与实战指南
前端·typescript·bun
2401_885885041 天前
易语言彩信接口怎么调用?E语言Post实现多媒体数据批量下发
前端
a1117761 天前
Three.js 的前端 WebGL 页面合集(日本 开源项目)
前端·javascript·webgl
Kk.08021 天前
项目《基于Linux下的mybash命令解释器》(一)
前端·javascript·算法
小李子呢02111 天前
前端八股---闭包和作用域链
前端
IT_陈寒1 天前
Redis的内存溢出坑把我整懵了,分享这个血泪教训
前端·人工智能·后端