如何使用两种不同的relativeTime自定义? [英] How do I use two different relativeTime customizations?

查看:165
本文介绍了如何使用两种不同的relativeTime自定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些地方(例如{m: "1m"})使用一个短的地方,而在其他地方(例如{m: "one minute"})使用一个长的地方.

I'd like to use a short one in some places (eg {m: "1m"}) and a long one in others (eg {m: "one minute"}).

推荐答案

我认为答案围绕此处记录的内容 简而言之,请使用函数来调整部分语言环境以满足您的需求.

I think the answer centres around what is documented here In short, tweak part of the locale to suit your needs using a function.

这里是一个简短的示例:

Here is a short sample:

var useshort = false;

moment.locale( 'en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "seconds",
        m:  function (/* number, withoutSuffix, key, isFuture */) { 'use strict';
         return useshort ? "1m" : "1 minute";
        },
        mm: "%d minutes",
        h:  "an hour",
        hh: "%d hours",
        d:  "a day",
        dd: "%d days",
        M:  "a month",
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

console.log(moment().subtract(1, "minute").fromNow() ); // 1 minute ago
console.log(moment().subtract(2, "minute").fromNow() ); // 2 minutes ago

useshort = true;
console.log(moment().subtract(1, "minute").fromNow() ); // 1m ago
console.log(moment().subtract(2, "minute").fromNow() ); // 2 minutes ago

由于您的需求约为'm',因此我们可以放心地忽略参数(我认为),因为这些参数未必会发生变化.我显示了2分钟的偏移量,以表明我们确实只修改了'm'的行为.

Since your need is about 'm', we can safely ignore the parameters (I think) as they are not suppose to change. I display the 2 minutes offset to show that we indeed only modified the behaviour of 'm'.

如果这对您来说太麻烦了(使用设置区域设置的范围内的变量),那么当然还有另一种选择,使您自己成为本地语言("en-short")并简单地对短代码进行硬编码值,然后根据需要设置语言环境. 我选择此解决方案是因为我不知道您是如何决定使用一种格式还是另一种格式的.

If this is too messy for you (using a variable from the scope of where the locale is set), an another option is, of course, to make your own local ('en-short') and simply hardcode the short value, then set the locale as you need. I chose this solution because I did not know how you had to make the decision of using one format vs the other.

要完整,这取决于您知道需要操纵哪个语言环境的事实.如果语言环境的选择取决于最终用户,则问题将变得更加复杂.在那种情况下,我所能想到的就是使用moment.localeData()._relativeTime检索当前语言环境的配置,并将'm'成员更改为适合您的函数.您仍然必须找出一种方法来在适当的语言环境中表达"m"和"minute"单位(不确定如何做到). 此解决方案实际上是不得已的方法,因为随着时间的推移,我不知道_relativeTime是否是安全的API(我想不是).

To be complete, this relies on the fact that you know which locale needs to be manipulated. If the choice of locale is up to the end user, the problem becomes a bit more complex. In that case, all I can think of would be to retrieve the configuration of the current locale using moment.localeData()._relativeTime and change the 'm' member to the function that works for you. You would still have to find out a way to express the 'm' and 'minute' units in the proper locale (not sure how to do that). This solution is really a last resort as I don't know if _relativeTime is a safe API over time (I would guess not).

希望这会有所帮助.

这篇关于如何使用两种不同的relativeTime自定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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