使用Moment.js进行语言环境检测 [英] Locale detection with Moment.js

查看:128
本文介绍了使用Moment.js进行语言环境检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Moment.js,格式设置如下:

I am using Moment.js in my project and formatting dates as follows:

var date = moment.unix(1318781876);
return date.format('LLLL');

当文档说明支持多种语言环境时.我想知道moment.js是否会自动检测语言环境,还是我需要检测语言环境并将其传递给moment?

The moment docs state the multiple locales are supported. I would like to know if moment.js will auto-detect the locale, or do I need to detect the locale and pass it to moment?

更新 我的目标是确保显示的日期采用用户所在地区的格式.即在美国,短日期格式为mm/dd/yy,而在英国为dd/mm/yy

Update My goal is to ensure the displayed date is in the format of the user's region. i.e. in the US the short date format is mm/dd/yy whereas in the UK it is dd/mm/yy

推荐答案

从momentjs文档开始:

As of momentjs documentation:

默认情况下,Moment.js带有英语语言环境字符串.如果你需要 其他语言环境,您可以将它们加载到Moment.js中以供以后使用.

By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js for later use.

您可以通过以下方式进行更改:

You can change it with that:

moment.locale(locale);

要使用javascript获取用户的语言环境,您可以执行以下操作:

To get the user's locale with javascript you can do that:

var locale = window.navigator.userLanguage || window.navigator.language;

请参阅: http://momentjs.com/docs/#/i18n/更改语言环境/

用于检测浏览器语言偏好的JavaScript

这篇关于使用Moment.js进行语言环境检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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