如何在momentJS中更改语言环境? [英] How to change locale in momentJS?

查看:946
本文介绍了如何在momentJS中更改语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的,但不起作用.

This is what I have but it does not work.

import moment from 'moment'
import 'moment/min/moment-with-locales'
import 'moment-duration-format' // used by moment

componentDidMount () {
  console.log(moment.locale())
  console.log(moment.locale('zh-cn')) 
  console.log(moment.locale()) 
}

控制台日志输出: en,en,en

console log outputs: en, en, en

预期的控制台日志输出:zh,zh-cn,zh-cn

expected console log output: en, zh-cn, zh-cn

当我尝试将导入时间从"moment"更改为导入时间从"moment/min/moment-with-locales"时,这会在此行上引发错误:

When I tried changing import moment from 'moment' to import moment from 'moment/min/moment-with-locales' but it throws an error on this line:

const total = moment.duration(this.props.stoveUsage.total, 'seconds').format('H:mm', { trim: false }) 

错误:momentWithLocales2.default.duration(...).format不是函数

error: momentWithLocales2.default.duration(...).format is not a function

推荐答案

所有您需要做的就是导入要使用的语言环境.在我的示例中,我想确保所有日期都使用西班牙语,这就是我所做的:

All you need to do is to import the locale you want to use. In my example, I wanted to make sure all the dates were in Spanish and this is what I did:

import moment from 'moment';
import 'moment/locale/es';

然后在需要时执行以下操作:

Then whenever you need it just do:

moment.locale('es')
moment(date).format("DD - MMMM - YYYY"

输出将是:

19 - julio - 2018

我想您将必须在应用程序中导入所需数量的语言环境.

I guess you will have to import as many locales as you need in your application.

这篇关于如何在momentJS中更改语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆