首页 > 解决方案 > 可以将变量传递给 angular.json 并避免代码重复吗?

问题描述

我正在寻找一种将信息传递到angular.json文件的方法,这样我就不需要重复构建配置并避免所有代码重复。我不能很好地解释,所以我会尝试一个例子。所以在我的angular.json下面configurations我有类似的东西

"configurations": {
"de": {
              "aot": true,
              "i18nLocale": "de",
              "i18nFile": "project/src/locale/messages.de.xlf",
              "i18nFormat": "xlf"
            },
            "en-gb": {
              "aot": true,
              "i18nLocale": "en-gb",
              "i18nFile": "project/src/locale/messages.en-gb.xlf",
              "i18nFormat": "xlf"
            },
            "en-us": {
              "aot": true,
              "i18nLocale": "en-us",
              "i18nFile": "project/src/locale/messages.en-us.xlf",
              "i18nFormat": "xlf"
            },
            "es": {
              "aot": true,
              "i18nLocale": "es",
              "i18nFile": "project/src/locale/messages.es.xlf",
              "i18nFormat": "xlf"
            },
            "fr": {
              "aot": true,
              "i18nLocale": "fr",
              "i18nFile": "project/src/locale/messages.fr.xlf",
              "i18nFormat": "xlf"
            },
            "it": {
              "aot": true,
              "i18nLocale": "it",
              "i18nFile": "project/src/locale/messages.it.xlf",
              "i18nFormat": "xlf"
            },
            "pt-br": {
              "aot": true,
              "i18nLocale": "pt-br",
              "i18nFile": "project/src/locale/messages.pt-br.xlf",
              "i18nFormat": "xlf"
            }

有没有办法让我们说一个变量i18n,我可以angular.json像这样使用它$i18n

"configurations": {
"i18n": {
              "aot": true,
              "i18nLocale": "$i18n",
              "i18nFile": "project/src/locale/messages.$i18n.xlf",
              "i18nFormat": "xlf"
            }

我英语不好。我希望我用这个例子很好地解释了我正在寻找的东西。提前致谢。

标签: jsonangularangular-cli

解决方案


我不会说angular,但希望这很有用,我想你正在看这样的东西:

echo '["de","en-gb","en-us","es","fr","it"]' | jq '
  {"configuration":[ .[] as $c |
    {
      ($c):{
         "aot":true,
         "i18nLocale": $c,
         "i18nFile": ("project/src/locale/messages." + $c + ".xlf"),
         "i18nFormat": "xlf"
      }
    }
  ]}'

根据您的第一个示例,此命令使用jq 命令行解析器来伪造 JSON 配置。

您可以将发送echo到的表扩展jq为任何国家/地区字符串并获得尽可能多的配置。


推荐阅读