gpt4 book ai didi

react-native - 如何在momentJS中更改语言环境?

转载 作者:行者123 更新时间:2023-12-04 05:13:10 26 4
gpt4 key购买 nike

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

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

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

当我尝试将导入时间从“moment”更改为导入时间,从“moment/min/moment-with-locales”导入时,在此行上引发了错误:
const total = moment.duration(this.props.stoveUsage.total, 'seconds').format('H:mm', { trim: false }) 

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

最佳答案

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

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

然后,只要您需要,请执行以下操作:
moment.locale('es')
moment(date).format("DD - MMMM - YYYY"

输出将是:
19 - julio - 2018

我猜您将需要在应用程序中导入所需的尽可能多的语言环境。

关于react-native - 如何在momentJS中更改语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com