如何使用ember-moment设置短月名称 [英] How set short month names using ember-moment

查看:85
本文介绍了如何使用ember-moment设置短月名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要暂时更改短短的一个月. 但是我做不到. 我已经尝试设置

I need to change short month in moment. But I can't do it. I have try to set

localeOutputPath: 'assets/moment-locales'

然后致电

Ember.$.getScript('/assets/moment-locales/ru.js');

在这种情况下,我有余烬迷航错误

In this case i have ember-mirage error

 Your Ember app tried to GET '/assets/moment-locales/ru.js?_=1490191145335',
 but there was no route defined to handle this request. Define a route that
 matches this path in your mirage/config.js file

设置短月份名称的简单方法是吗?

Is it simple way to set short months name for moment?

推荐答案

我假设您正在使用ember-moment插件;并且已经用

I assume you are using ember-moment addon; and already have configured config/environment.js with

moment: {
  // This will output _all_ locale scripts to assets/moment-locales
  localeOutputPath: 'assets/moment-locales'
},

正如您所提到的.

Ember.$.getScript('/assets/moment-locales/ru.js');

提供了一种在需要时动态地动态加载ru区域设置的方法.这意味着,与其在应用程序的javascript文件中不包含相关语言环境,而是希望在应用程序中的某些用户请求下加载相关语言环境.通常,最好在路由器的挂钩方法(例如beforeModelmodel)中执行此类加载操作.

provides a way to dynamically load moment ru locale on the fly when needed. This means instead of including the related locale to your application's javascript file you prefer to load relevant locale upon some user request in your application. Generally it is best to perform such loading operation within a router's hook methods such as beforeModel or model.

为了从moment获取短月份的名称;您需要先通过

In order to get short month names from moment; you need to first import ES6 moment module via

import moment from 'moment';

并使用

moment.monthsShort()

据我所见;您请求语言环境的方式存在问题,因此您遇到了所提到的错误.我认为从纯文本来看,可以工作的代码是更好的解释;因此,我创建了以下 git存储库,以说明如何进行更改路由中的语言环境,以及如何显示从moment检索的短名称.请通过克隆并在您的本地主机中运行来查看它.

As far as I can see; there is a problem with the way you are requesting the locale so you are getting the error you have mentioned. I believe a working code is a better explanation from pure text; hence I have created the following git repository to illustrate how you can change the locale dynamically in a route and how you can display short names retrieved from moment. Please take a look at it by cloning and running in your localhost.

在此存储库中的应用程序中,application.hbs包含指向5个子路由的链接;每个都以不同的语言显示月份的简称.动态加载相关语言环境的技巧的代码位于routes/locale-route.js文件的model hook方法中.如果已经加载了语言环境(请注意,moment默认包含英语),则只需切换到目标语言环境(moment.locale(localeToLoad);)即可返回月份的简称.否则,它将对服务器执行远程调用,并等待响应(通过使用Promise)以返回月份名称. 5种不同语言的所有路由均从该基本路由扩展而来.一次,从服务器加载语言环境.您不再需要再次加载它,并且locale-route已经按照我的解释进行了处理.希望对您有所帮助.

In the application at this repository, application.hbs contains links to 5 sub-routes; each displaying short names of months in different languages. The code that does the trick of dynamically loading the relevant locale is in routes/locale-route.js file's model hook method. If locale is already loaded (note that English is included by default with moment) it simply returns the short names of the months via switching to target locale (moment.locale(localeToLoad);). Otherwise, it performs a remote call to the server and waits for the response (by using a promise) to return the name of months. All routes for 5 different languages extend from this base route. Once, a locale is loaded from the server; you no longer need to load it again once more and the locale-route already handles it as I explained. I hope that helps.

阅读您的评论后;我更新了源代码以包含ember-cli-mirage. Mirage是一个客户端模拟服务器,用于开发,测试和原型化您的应用程序.一旦将其包含为依赖项,它就会开始拦截您的远程呼叫请求.因此,在您的情况下,海市rage楼拦截了要求苛刻的相关区域设置的请求.您需要做的是遍历海市local楼.为此,您需要添加以下

After reading your comment; I updated the source code to include ember-cli-mirage. Mirage is a client-side mock server to develop, test and prototype your application. Once you include it as a dependency it starts intercepting your remote call requests. Hence, in your case mirage intercepts requests for demanding related locale. What you need to do is passing through mirage for locales. In order to do that, you need to add following

this.passthrough('/assets/moment-locales/**');

mirage/config.js,以使海市rage楼在运行时不会干扰要求严格的瞬时位置.请参阅相关文件在我提供的git存储库下.这肯定会解决您的问题.

to mirage/config.js so that mirage will not interfere with demanding moment-locales at the runtime. Please see related file under the git repository I have provided. This will solve your problem for sure.

这篇关于如何使用ember-moment设置短月名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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