可以将Variable传递给angular.json并避免代码重复吗?

时间:2018-11-12 13:17:13

标签: json angular angular-cli

我正在寻找一种将信息传递到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"
            }

我英语不好。我希望我可以使用示例很好地解释我要寻找的内容。提前致谢。

1 个答案:

答案 0 :(得分:1)

我不会说 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 command line parser伪造JSON配置。

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