javascript 使用带有 lang 文件和 require.js 的 moment.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16165239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:32:00  来源:igfitidea点击:

Using moment.js with lang file and require.js

javascriptrequirejsmomentjs

提问by John Sieb

I'm currently trying to use the moment.js library with require.js and I'm still having trouble understanding the correct setup of such a project. Here is what I do in my main.js file:

我目前正在尝试将 moment.js 库与 require.js 一起使用,但我仍然无法理解此类项目的正确设置。这是我在 main.js 文件中所做的:

requirejs.config({
    baseUrl: 'app',

        paths: {
            // ... more parameters (all Backbone related)
            'moment': 'lib/moment',
            'moment_de': 'lib/lang/de',
        },

    shim: {
        'moment' : {
            deps: [],
        },

        'moment_de': {
            deps: ['moment'],
        },

        // ... more parameters (all Backbone related)
    }
});

I'm using a separate module for configuration purposes. The module looks like this:

我正在使用一个单独的模块进行配置。该模块如下所示:

define(['moment', 'moment_de'], function(moment, de) {

    moment.lang('de');

    var configuration = {}
    // ...    
    return configuration;
});

As you can see, I'm trying to change the global language of the moment object in this file, but I'm running into the following error messages:

如您所见,我正在尝试更改此文件中 moment 对象的全局语言,但遇到以下错误消息:

Uncaught Error: Module name "../moment" has not been loaded yet for context: _. Use require([])

And later on:

后来:

Uncaught TypeError: Cannot call method 'preparse' of undefined 

The first error message is the language module which is being loaded although it should be loaded AFTER the moment module (if I'm doing it right). The second one is from the moment module that is trying to switch to the language module which hasn't been loaded.

第一条错误消息是正在加载的语言模块,尽管它应该在 moment 模块之后加载(如果我做对了)。第二个来自于试图切换到尚未加载的语言模块的时刻模块。

Could someone please shine some light on this issue. Thanks in advance.

有人可以对这个问题有所了解。提前致谢。

EDIT: I fixed the problem using the minified language versions (e.g. this one). Apparently the minified versions are using the AMD format, which allow for an easier inclusion in require.js projects).

编辑:我使用缩小的语言版本(例如这个)解决了这个问题。显然,缩小版本使用的是 AMD 格式,这使得更容易包含在 require.js 项目中)。

I still don't quite understand why it is not possible to include the languages using the shim config, though. Maybe someone could try to explain that.

不过,我仍然不太明白为什么不能使用 shim 配置来包含语言。也许有人可以尝试解释一下。

回答by mate64

Another solution (2015):

另一个解决方案(2015):

This example aims to demonstrate how to use the moment.jstranslations with the navigator.languageproperty, usually the preferred language of the user.

此示例旨在演示如何使用moment.js带有navigator.language属性的翻译,通常是用户的首选语言。

Define moment.jsand the language files separately in your requirejsconfig, like this:

moment.js在您的requirejs配置中分别定义和语言文件,如下所示:

require.config({
  config: {
    'moment': {
      noGlobal: true
    }
  },
  paths: {
    ...
    'moment': '../vendor/moment/moment',
    'moment_de': '../vendor/moment/locale/de',
    'moment_pl': '../vendor/moment/locale/pl'
    ...
  },
  ...
});

Create a small module, like lib/moment.jsand specify your language configuration (you can find a list of RFC 4646 language tags here):

创建一个小模块,例如lib/moment.js并指定您的语言配置(您可以在此处找到 RFC 4646 语言标签列表):

define(function(require) {
  'use strict';

  var moment = require('moment'), locale;

  switch(navigator.language) {
    case 'de':
    case 'de-at':
    case 'de-de':
    case 'de-li':
    case 'de-lu':
    case 'de-ch':
      locale = 'moment_de';
    break;

    case 'pl':
      locale = 'moment_pl';
    break;

    ...
  }

  if (locale) {
    require([locale]);
  }

  return moment;
});

Please notice: moment.jssupports English by default.

请注意:moment.js默认支持英文。



In your chaplinjsview class (or any other mvcclass/plain script etc), use it like this:

在您的chaplinjs视图类(或任何其他mvc类/纯脚本等)中,像这样使用它:

define([
  'chaplin'
  'lib/moment'
], function(Chaplin, moment) {
  'use strict';

  var MyView = Chaplin.View.extend({

    ...

    parse: function() {
      ...
      console.log(moment().format('LLL'));
      ...
    }

    ...

  });

  return MyView;
});

回答by Brian Lewis

require({
    paths: {
        'moment': '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/moment.min',
        'moment_de': '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/lang/de.min'
    }
}, ['moment', 'moment_de'], function(moment){
    moment.lang('de');
    alert(moment(1316116057189).fromNow());
});

You shouldn't need to shim the modules, as moment.js supports AMD. http://jsfiddle.net/moderndegree/xYXUC/

您不需要填充模块,因为 moment.js 支持 AMD。 http://jsfiddle.net/moderndegree/xYXUC/