[英]How can I build a 'moment' function that works in a non-global locale?
With MomentJS, if necessary I can set a locale on a specific moment instance (rather than globally) using .locale
(or .lang
in older versions). 使用MomentJS,如有必要,我可以使用
.locale
(或旧版本中的.lang
在特定时刻实例(而不是全局)上设置语言环境。 How can I create a function equivalent to moment
(all 42 signatures of it) that always works in a specific locale that's different from the global? 我如何创建一个等效于
moment
的函数(它的所有42个签名),该函数始终在不同于全局的特定语言环境中工作?
I have a submodule within a web project that has to work with a custom locale. 我在Web项目中有一个必须与自定义语言环境一起工作的子模块。 We don't want this locale used outside this particular submodule, but we want it used consistently within that submodule.
我们不希望在此特定子模块之外使用此语言环境,但希望在该子模块内一致地使用它。 So I basically want a
moduleMoment
(or whatever) that I can call just like moment
, but which works in the custom locale. 所以我基本上想要一个我可以像
moment
一样调用的moduleMoment
(或任何东西),但是它可以在自定义区域设置中工作。 Is there any built-in way to do that? 有内置的方法吗?
Note : I'm not talking about changing the locale of an existing instance, I'm talking about calling the moment
function (in all its various different signatures) using a locale other than its global locale. 注 :我不是在谈论改变现有实例的语言环境,我说的是调用
moment
使用比其全球区域以外的区域设置功能(在其所有的各种不同的签名)。 So this won't work: 所以这行不通:
var m = moment(args).locale(localeId);
...because the args will be processed in the global locale, and then the locale gets changed on the instance after the fact, which is too late. ...因为将在全局语言环境中处理args,然后事后在实例上更改语言环境,为时已晚。
The only way I can find to do it is to have a function that temporarily sets the locale, calls moment
, then sets the locale back to the previous one, like this: 我能找到的唯一方法是拥有一个临时设置语言环境的函数,调用
moment
,然后将语言环境设置回前一个,如下所示:
function moduleMoment() {
var prevLocaleId, m;
prevLocaleId = moment.locale();
try {
moment.locale(customLocaleId);
m = moment.apply(this, arguments);
return m;
} finally {
moment.locale(prevLocaleId);
}
}
Or one could add it to moment
, like this: 或者可以将其添加到
moment
,如下所示:
moment.localized = function(locale) {
var prevLocaleId, m, args;
prevLocaleId = moment.locale();
try {
moment.locale(locale);
m = moment.apply(this, Array.prototype.slice.call(arguments, 1));
return m;
} finally {
moment.locale(prevLocaleId);
}
};
Unfortuntately it doesn't seem like Moment supports this functionality. 不幸的是,Moment似乎不支持此功能。 There is a new feature request in the GitHub issue tracker: https://github.com/moment/moment/issues/2291
GitHub问题跟踪器中有一个新功能请求: https : //github.com/moment/moment/issues/2291
You could write a function that forwards its arguments to the moment
constructor and then applies the language you want.您可以编写一个函数,将其参数转发给 moment
构造函数,然后应用所需的语言。
Here's an example: 这是一个例子:
function localizedMoment() { return moment.apply(null, arguments).locale("fr"); }
If you need to allow a parameter, just use Array.prototype.slice
:如果需要允许参数,只需使用 Array.prototype.slice
:
function localizedMoment(locale) { var args = Array.prototype.slice.call(locale, 1); return moment.apply(null, args).locale(locale); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.