0

forexample I have {moment().locale('en').format('MMMM Do YYYY')} in my App.js. But I want it switch on {moment().locale('fr').format('MMMM Do YYYY')} while I am switching languege in whole website.

Something like this:

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;

1 Answers1

0

You can reach the current format locale language on pc with ;

const locale = new Intl.NumberFormat().resolvedOptions().locale;

If you want to switch it on all website, need to have a locale value in global as you had (currentLang). Then use this value insted of locale('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")
Onur Doğan
  • 346
  • 2
  • 8