Introduction react-i18next is a powerful internationalization framework based on
Install You need to install both Configuration Create a new Create three new files in
en.json { "header": { "register":"Register", "signin":"Sign In", "home": "Home" }, "footer": { "detail" : "All rights reserved @ React" }, "home": { "hot_recommended": "Hot Recommended", "new_arrival": "New arrival", "joint_venture": "Joint Venture" } } zh.json { "header": { "register":"Register", "signin":"Login", "home": "Home" }, "footer": { "detail" : "Copyright @ React" }, "home": { "hot_recommended": "Hot Recommended", "new_arrival": "New arrival", "joint_venture": "Joint venture" } } config.ts import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import translation_en from './en.json'; import translation_zh from './zh.json'; const resources = { en: translation: translation_en, }, en: { translation: translation_zh, }, }; i18n.use(initReactI18next).init({ resources, lng: 'zh', interpolation: escapeValue: false, }, }); export default i18n; useReference Profile Reference the import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import './i18n/config'; // Reference configuration file ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, document.getElementById('root') ); Use in componentsMethod 1 Use the import React from 'react'; import styles from './Home.module.css'; //Introduce HOC high-order function withTranslation and i18n ts type definition WithTranslation import { withTranslation, WithTranslation } from "react-i18next" class HomeComponent extends React.Component<WithTranslation> { render() { const { t } = this.props; return <> <h1>{t('header.home')}</h1> <ul> <li>{t('home.hot_recommended')}</li> <li>{t('home.new_arrival')}</li> <li>{t('home.joint_venture')}</li> </ul> </> } } export const Home = withTranslation()(HomeComponent); // Use the withTranslation high-order function to complete the data injection of language configuration Method 2 Use import React from 'react'; import { useTranslation, Trans } from 'react-i18next' export const Home: React.FC = () => { const { t } = useTranslation() return ( <div> <h1>{t('header.home')}</h1> <ul> <li>{t('home.hot_recommended')}</li> {/* There is another way */} <li><Trans>home.new_arrival</Trans></li> </ul> </div> ); }; Switch language import i18n from 'i18next'; const changeLanguage= (val) => { i18n.changeLanguage(val); // val parameter value is 'en' or 'zh' }; or import React from 'react'; import { useTranslation } from 'react-i18next' export const Home: React.FC = () => { const { t, i18n } = useTranslation() return ( <button onClick={()=>i18n.changeLanguage(i18n.language=='en'?'zh':'en')}>{i18n.language=='en'?'zh':'en'}</button> ); }; This is the end of this article about React internationalization react-i18next. For more relevant React internationalization react-i18next content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Sample code for achieving three-dimensional picture placement effect with pure CSS
>>: Summary of MySQL log related knowledge
Table of contents 1. Routing animation 2. Group Q...
When we use the MySQL service, under normal circu...
Nginx (engine x) is a high-performance HTTP and r...
Preface The previous article installed Hadoop, an...
Table of contents 1. Vertical (longitudinal) slic...
Table of contents MySQL Truncate usage 1. Truncat...
Problem: The website published through IIS is pla...
Problem to be solved Mainly for cross-level commu...
<iframe src=”test.jsp” width=”100″ height=”50″...
This article describes how to install mysql5.6 us...
My97DatePicker is a very flexible and easy-to-use...
Using depends_on to sort containers does not perf...
Table of contents Immediately execute function fo...
GitHub address: https://github.com/dmhsq/dmhsq-my...
I was bored and sorted out some simple exercises ...