例如,我的App.js中有{moment((.locale('en'(.format('MMMMDo YYYY'(}。但我希望它在整个网站切换语言时打开{moment。
类似这样的东西:
import React from 'react'
import {useTranslation} from "react-i18next";
import moment from 'moment';
const {i18n} = useTranslation();
const {languegeswitcher} = i18n;
const languegeswitcher = () =>
{
if (currentLang === "en") {
return {moment().locale('en').format('MMMM Do YYYY')};
} else if (currentLang === "fr"){
return {moment().locale('fr').format('MMMM Do YYYY')};
} else (currentLang === "ru")
return {moment().locale('ru').format('MMMM Do YYYY')};
}
export default languegeswitcher;
您可以在pc上使用;
const locale = new Intl.NumberFormat().resolvedOptions().locale;
如果你想在所有网站上切换它,需要在global中有一个区域设置值(currentLang(。然后使用此值代替区域设置('en'(
var localDate = moment(date)
localDate.locale = locale // localDate.locale(locale)
// or use your global value == localDate.locale(currentLang) , localDate.locale = currentLang
localDate.format("MMMM D YYYY")