【问题标题】:Moment-Timezone is using default locale after global Moment locale is setMoment-Timezone 在设置全局 Moment 语言环境后使用默认语言环境
【发布时间】:2017-11-01 10:02:26
【问题描述】:

我正在构建一个使用 Typescript 编写的应用程序,它使用 Moment.js 和 moment-timezone 的功能。我需要本地化应用程序中的日期和时间戳,因此在主 app.ts 文件中,我使用设备的语言设置了时刻的区域设置。

更新:这里是带有附加 cmets 的示例文件的要点 https://gist.github.com/spstratis/fa853f9750a095d4acd0d1196a285be9

app.ts

import * as moment from 'moment/min/moment-with-locales';

let language = appUtil.getPhoneLanguage();

moment.locale(language);

// the expected locale is printed
console.log("Moment Locale = " + moment.locale());

问题是,在此实用程序模块中,当我导入 moment-timezone 时,即使我在主 app.ts 文件中全局设置了 moment 的语言环境,它也默认为“en”语言环境。

以下是我的两个实用方法,如果 moment-timezone 将它们默认为“en”,我如何本地化相对日期字符串和月份?

我尝试将 .locale(locale) 添加到 moment 方法,但这并没有改变任何东西。如果我导入了对某些方法有效但在任何需要使用时区实用程序的方法上失败的时刻而不是时刻时区。

date-util.ts

import * as moment from 'moment-timezone';

export function dateRelativeTime(value): string {
  let timezoneId = appCache.getTimezoneId();
  let localTime = _getLocalUtcDateString(value, timezoneId);
  let dateMoment = moment(localTime, "MM/DD/YYYY hh:mm:ss A");
  let formatedDate = dateMoment.tz(timezoneId).fromNow();

  return formatedDate;
};

export function localizedMonths(): ValueList {
  let m = moment("2016");
  let months = new ValueList([]);
  for (var i = 0; i < 12; i++) {
    months.push({ ValueMember: [i + 1], DisplayMember: m.month(i).format('MMMM') });
  }

  return months;
};

【问题讨论】:

  • 你能用 TS 和两个 moment 库设置一个要点吗?
  • @BogdanBiv 使用了我所有的代码?
  • 附带说明:您可以使用localeData = moment.localeData(); localeData.months(); 获取月份名称的本地化数组,请参阅文档中的Accessing locale specific functionality
  • @Stavros_S:这正是重点:将您的问题隔离在您可以共享的较小项目中。
  • @VincenzoC - 或者只是moment.months()

标签: javascript date datetime typescript momentjs


【解决方案1】:

我不知道为什么,但我突然遇到了同样的问题。
在使用带有区域设置的时刻和时刻时区时,您似乎需要一些额外的配置。

这是您仅使用一种语言环境时的最低设置。

import 'moment/locale/ja' // or whatever locale you want
import moment from 'moment'
import memontTZ from 'moment-timezone'
mementTZ.defineLocale('ja', moment.localData()._config)

我相信您可以按照以下示例配置多个语言环境。
参考:https://github.com/moment/moment-timezone/issues/647#issuecomment-439600573

【讨论】:

    【解决方案2】:

    我在 Typescript 中遇到了同样的问题。我想使用 moment-timezone 将时区设置为 Europe/Brussels 并将语言环境设置为比利时荷兰语,我这样解决了:

    import 'moment/locale/nl-be';
    import * as momentTZ from 'moment-timezone';
    momentTZ.locale("nl-be");
    momentTZ.tz.setDefault("Europe/Brussels");
    

    如果你想在你的项目中使用 momentTZ,现在使用 momentTZ,就像这个 f.e.

    const exampleDate = momentTZ().format('dddd D MMMM');
    

    【讨论】:

      【解决方案3】:

      您正在导入错误的时刻。不要这样做:

      import * as moment from 'moment/min/moment-with-locales';
      

      这样做:

      import * as moment from 'moment';
      

      它将在您使用时加载各个语言环境(在 Node.js 上),然后您将共享时刻时区使用的相同时刻依赖项,因此您的全局语言环境将继续执行。

      【讨论】:

      • 这实际上是我最初导入它的方式。但它没有用。仅在导入时刻时,即使在设置区域设置之后也是如此。如果我之后立即注销全局语言环境,即使尝试将其设置为“es”,它也会打印出“en”
      • 您还需要导入所需的每个本地,如文档中所示。除非您在 Node.js 上运行它,否则它将在您调用它们时异步加载它们。
      • 我仍然没有运气,即使复制了文档显示的内容。
      • 这些here 不适合你?
      • 他们不是,我已经更新了我的要点以展示我如何加载它的示例。 gist.github.com/spstratis/fa853f9750a095d4acd0d1196a285be9
      【解决方案4】:

      你试过.format('')吗?

      moment.locale();         // en
      moment().format('LT');   // 6:27 PM
      moment().format('LTS');  // 6:27:51 PM
      moment().format('L');    // 05/31/2017
      moment().format('l');    // 5/31/2017
      moment().format('LL');   // May 31, 2017
      moment().format('ll');   // May 31, 2017
      moment().format('LLL');  // May 31, 2017 6:27 PM
      moment().format('lll');  // May 31, 2017 6:27 PM
      moment().format('LLLL'); // Wednesday, May 31, 2017 6:27 PM
      moment().format('llll'); // Wed, May 31, 2017 6:27 PM
      

      更新:还要确保您的时刻库是其中包含语言环境的库: https://momentjs.com/ 指定有两个矩库,带 (moment-with-locales.js) 和不带语言环境 (moment.js)。

      【讨论】:

      • 是的 - 但它仍然以英文返回值。即使我设置了 moment.locale("es")。在我看来,时刻时区不尊重其他文件中时刻设置的语言环境......我正在创建一个要点来帮助更好地解释事情。
      • 我为这个问题添加了一个要点。你会注意到我已经在我的导入中使用了moment-with-locales。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多