【问题标题】:How do I 'ng serve' the original app after localizing it with @angular/localize?使用 @angular/localize 本地化原始应用程序后,如何“服务”原始应用程序?
【发布时间】:2020-10-21 00:37:03
【问题描述】:

我正在使用 Angular 的本地化 @angular/localize 并在配置它以将应用程序翻译成不同的语言后我尝试使用“ng serve”来提供它的默认版本但我收到了这个错误:

An unhandled exception occurred: The development server only supports localizing a single locale per build.

我基于此尝试使用不同的端口:How to serve different Angular locales during development using ng serve?,但无济于事。

对于翻译版本,一切工作使用“ng serve --configuration=fr”。 构建 work 在 dist/App 文件夹中正确创建“en-US”和“fr”文件夹。

当我删除 dev-server 中的 inlineLocales.size 条件时,一切正常,没有错误:

if (i18n.shouldInline && tsConfig.options.enableIvy !== false) {
    //if (i18n.inlineLocales.size > 1) {
    //    throw new Error('The development server only supports localizing a single locale per build');
    //}
    await setupLocalize(i18n, browserOptions, webpackConfig);
}

所以可能是 Angular-devkit 的问题。

我尝试通过创建新的“en-US”语言环境来解决此问题,但收到此错误:

An unhandled exception occurred: An i18n locale ('en-US') cannot both be a source locale and provide a translation.

仅使用 'en' 而不是 'en-US' 并使用 <source><target> 翻译创建新的翻译文件后,它使用 'ng serve --configuration=en' 工作,但它看起来像错误的解决方案。

这是我的 angular.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "App": {
      "projectType": "application",
      "i18n": {
        "sourceLocale": "en-US",
        "locales": {
          "fr": "src/locale/messages.fr.xlf"
        }
      },
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "localize": true,
            "outputPath": "dist/App",
            "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": {
            "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": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ]
            },
            "fr": {
              "localize": ["fr"]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "App:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "App:build:production"
            },
            "fr": {
              "browserTarget": "App:build:fr"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "App:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular: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": "App:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "App:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "App"
}

如何重现: 该应用程序是使用 Angular CLI 10.0.0 和 Angular 10.0.1 生成的,然后通过 'ng add @angular/localize' 添加 @angular/localize。在 app.component.html 中是单个 <h1 i18n="@@app.h1">Hello</h1>。使用上面的 angular.json。

【问题讨论】:

    标签: angular angular-i18n angular-localize


    【解决方案1】:

    无需设置localize: false。这里缺少的是在您的angular.json 中为您的sourceLocale 语言添加几个条目,在本例中为en-US(即使您没有en-US 的专用翻译文件)。

    前往angular.json,留下configurations 对象,如:

    ...
    "fr": {
      "localize": ["fr"]
    },
    "en-US": {
      "localize": ["en-US"] << or whatever your sourceLocale is
    },
    ...
    

    然后:

    "fr": {
      "browserTarget": "App:build:fr"
    },
    "en-US": {
      "browserTarget": "App:build:en-US" << or whatever your sourceLocale is
    },
    

    现在您应该能够:

    ng serve --configuration=en-US
    

    或者

    ng serve --configuration=fr
    

    您可以通过编辑 package.json scripts 来简化:

    "scripts": {
      "ng": "ng",
      "start": "ng serve --configuration=en-US",
      "start:fr": "ng serve --configuration=fr",
    ...
    

    现在,npm run start 将提供英文版应用程序,npm run start:fr 将提供法文版应用程序。

    【讨论】:

    • 您还可以用逗号组合多个配置。但是,专门针对serve,我发现我必须使用浏览器目标来代替,例如ng serve myapp --browser-target=myapp:build:local,en-US --open --port=4200ng serve myapp --browser-target=myapp:build:local,es-MX --open --port=4201。这将同时使用我的“本地”配置和正确的文化配置。见this
    【解决方案2】:

    Set "localize": false in your example in your answer is equal to "localize": ["en"],

    所以...

    虽然接受的答案有效,但它不是正确的答案,因为您不应该将默认行为作为所有语言的唯一编译。

    您可以做些什么来为该语言构建并调用它,所以最终您将拥有:

         "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          ....
          "configurations": {
            .......
            "fr": {
              "localize": ["fr"]
            },
            "en": {
              "localize": ["en"]
            },
          }
        },
    
    "serve": {
            ...
            ...
            "fr": {
              "browserTarget": "App:build:fr"
            },
            "en": {
              "browserTarget": "App:build:en"
            }
    
          }
        },
    

    然后,当您执行ng serve --configuration=en 时,它会起作用,并且您无需将默认构建选项更改为所有其他构建。

    【讨论】:

      【解决方案3】:

      在“options”中,必须将“localize”设置为false:

      "architect": {
          "build": {
            "builder": "@angular-devkit/build-angular:browser",
            "options": {
              "localize": false,
      

      然后重新构建:

      ng build
      

      【讨论】:

      • 你不应该为了使用不同的语言环境而来回更改 angular.json
      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多