简体   繁体   中英

Using translation react-i18next with react-moment

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM