【问题标题】:Angular 9 internationalizationAngular 9 国际化
【发布时间】:2020-03-04 11:16:28
【问题描述】:

我正在尝试使用Angular 9 中的新国际化方式来更改我们现有的应用程序。这是我在angular.json 中的配置方式:

{
...
"projects": {
    "viewer": {
      ...
      "i18n": {
        "sourceLocale": "en-US",
        "locales": {
          "bg": "src/locale/messages.bg.xlf"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            },
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "assets": [
              ...
            ],
            "styles": [
              ...
            ],
            "scripts": [
              ...
            ],
            "i18nMissingTranslation": "error"
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            },
            "bg": {
              "localize": ["bg"]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "viewer:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "viewer:build:production"
            },
            "bg": {
              "browserTarget": "viewer:build:bg"
            }
          }
        },
        "extract-i18n": {
          ...
        },
        "test": {
          ...
        },
        "lint": {
          ...
        }
      }
    },
    "viewer-e2e": {
      ...
    }
  },
  ...
}

但是当我运行ng serve --configuration=bg 时,它仍然提供默认的en 翻译。有什么想法吗?

【问题讨论】:

  • “viewer:build:bg”中的“viewer”是什么?你的项目名称是“my-project”,是不是打错了?
  • @AkshayRana 是的,对不起,这是一个错字。项目名称为viewer

标签: angular internationalization


【解决方案1】:

尝试在您的angular.json 文件中将aot 设置为true。如果没有这个,Angular 将使用 JIT 模式,您需要使用 TRANSLATIONS 令牌动态提供翻译。

"architect": {
    "build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
        "aot": true,
        "customWebpackConfig": {
          "path": "./extra-webpack.config.js"
        },

【讨论】:

    【解决方案2】:

    我目前遇到了相同的流程。到目前为止我的理解:

    • Angular 9“本地化”要求构建配置(例如“生产”)和本地化配置(例如“bg”)
    • 只有将两者叠加才能得到想要的构建,即ng build --configuration=production,bg
    • 通过在“i18n”中定义“语言环境”列表,您可以使用非常方便的--localize 指令来叠加定义为“语言环境”的每个配置

    因此通过运行ng build --configuration=production --localize(或--prod),构建将以“生产”配置为基础,然后将i18n.locales中定义的每个语言环境叠加在其上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2017-06-09
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多