简体   繁体   English

Angular 元素和国际化 (i18n)

[英]Angular elements and Internationalization (i18n)

I am working in a project that has Angular elements(or Angular web components) and I need do the Internationalization (i18n) in Spanish language following the Angular rules. I am working in a project that has Angular elements(or Angular web components) and I need do the Internationalization (i18n) in Spanish language following the Angular rules.

my angular.json is:我的angular.json是:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-component": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "i18n": {
          "sourceLocale": "en",
          "locales": {
            "es": "src/locale/messages.es.xlf"
          }
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "ngx-build-plus:build",
          "options": {
            "outputPath": "dist/my-component",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "es": {
              "localize": ["es"]
            },
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "ngx-build-plus:dev-server",
          "options": {
            "browserTarget": "my-component:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-component:build:production"
            },
            "es": {
              "browserTarget": "my-component:build:es"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-component:build"
          }
        },
        "test": {
          "builder": "ngx-build-plus:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-component:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-component:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "my-component"
}

The problem appears when I try to build the project.当我尝试构建项目时出现问题。 When I execute this command ng build --prod --output-hashing none --single-bundle true for build the project(for angular elements) all its fine but when I try to build the translation using this command ng build --configuration=es this error appears in console:当我执行此命令ng build --prod --output-hashing none --single-bundle true用于构建项目(对于 angular 元素)时,一切都很好,但是当我尝试使用此命令构建翻译时ng build --configuration=es这个错误出现在控制台:

An unhandled exception occurred: Requested locale 'es' is not defined for the project.
See "/private/var/folders/yx/4vbvv8pd5r17fw8v08kxzt4m0000gq/T/ng-Sd34ew/angular-errors.log" for further details.

Any idea of how resolve it?知道如何解决吗?

You need to move the i18n section outside of the schematics node您需要将i18n部分移到schematics节点之外

"my-component": {
  "projectType": "application",
  "schematics": {
    "@schematics/angular:component": {
      "style": "scss"
    }
  },
   "i18n": {
      "sourceLocale": "en",
      "locales": {
        "es": "src/locale/messages.es.xlf"
      }
    },
  "root": "",
  "sourceRoot": "src",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM