Using translation react-i18next with react-moment

1.2k views Asked by At

I have english and swedish in my react web application.

I am using react-moment and in my App.js, I have imported moment/locale and placed import Moment from 'react-moment' for every page that I need to have the dates.

import 'moment/locale/sv'
import 'moment/locale/en-gb'

How can I translate it to swedish but not permanently? The user can switch language from english to swedish and vice versa.

   <Moment format="ddd DD MMM">
     {flight.date}
   </Moment>

  <Moment format="HH:mm">{stop.arrival}</Moment>
1

There are 1 answers

0
anttud On BEST ANSWER

Change locale with moment.locale() and set local to Moment Element

moment.locale('sv') 
<Moment format="ddd DD MMM" local>
 {flight.date}
</Moment>

Or you can also do

<Moment locale="sv" format="ddd DD MMM">{flight.date}</Moment>