1

我有一个似乎无法解决的问题。我尝试了几种不同的方法,但还没有任何效果。

我正在使用 grunt-messageformat 创建我的 i18n 本地化副本。我有 2 个包含语言的文件夹,我想让 grunt 自动为每个文件夹(语言)生成正确的输出。

让我最接近的任务是:

grunt.registerTask("ReadFolders", "Read the language folders in app/data/i18n/", function () {
  // Returns an array of the paths to the language folders.
  // ['app/data/i18n/en', 'app/data/i18n/key', ...]
  var languageFolders = grunt.file.expand("app/data/i18n/*");
  var path;
  var languageName;
  var i;

  for (i = 0; i < languageFolders.length; i++) {
    path = languageFolders[i];
    languageName = path.substring(path.lastIndexOf("/") + 1, path.length);

    grunt.config.set("mFormat.locale", languageName);
    grunt.config.set("mFormat.inputdir", "app/data/i18n/" + languageName);
    grunt.config.set("mFormat.output", "app-dist/test/js/locales/" + languageName + "/i18n.js");

    grunt.task.run("messageformat:all");
  }
});

这也为我在 initConfig 中设置的 messageformat 任务使用了以下代码:

messageformat: {
  all: {
    locale: "<%= mFormat.locale %>",
    inputdir: "<%= mFormat.inputdir%>",
    output: "<%= mFormat.output%>"
  }
}

问题是我在“readFolders”中的循环在 messageFormat 任务运行之前运行了两次,这意味着该任务运行了两次,但两次都使用 mFormat 变量的最后一个值。

我没有看到任何有关如何访问使用 initConfig 设置的任务回调的示例。

有什么想法吗?还是其他想法?

谢谢

4

1 回答 1

1

好吧,我没有找到一种方法来做我最初要求的事情......但我找到了一个很好的解决方法来满足我的需求。我没有为每个文件夹运行任务,而是为每种语言动态重写了 messageformat 配置。

grunt.registerTask("ReadFolders", "Read the language folders in app/data/i18n/", function () {
  // Returns an array of the paths to the language folders.
  // ['app/data/i18n/en', 'app/data/i18n/key', ...]
  var languageFolders = grunt.file.expand("app/data/i18n/*");
  var path;
  var languageName;
  var locale;
  var messageFormat = {};
  var i = 0;

  for (i = 0; i < languageFolders.length; i++) {
    path = languageFolders[i];
    languageName = path.substring(path.lastIndexOf("/") + 1, path.length);

    locale = languageName;
    if (languageName === "key") {locale = "en"; }

    messageFormat[languageName] = {
      locale: locale,
      inputdir: "app/data/i18n/" + languageName,
      output: "app-dist/test/js/locales/" + languageName + "/i18n.js"
    };

  }

  grunt.config.set("messageformat", messageFormat);

  grunt.task.run("messageformat");
});

经过相当详尽的搜索,我认为这是唯一可能的(坦率地说,就我而言,这是更好的)解决方案。

如果有人有任何其他想法,仍然很高兴听到任何其他想法。

于 2013-09-18T20:13:11.397 回答