[英]angular.json setup for multiple i18n locales
我的项目设置是为两个语言环境构建的,每个语言环境都有自己的 url(通过configuration.<locale>.baseRef
)。 这对于构建项目(通过ng build
)工作正常,尽管此设置使我的开发服务器无法使用。 如果我尝试运行npm start
,我会收到以下错误:
An unhandled exception occurred: The development server only supports localizing a single locale per build
这使得我必须从projects.<app-name>.architect.build.options.localize
选项中删除构建名称(开发服务器将从["en-US"]
设置开始,但不是["en-US", "es-ES"]
)
我知道它很冗长,但我在下面包含了 angular.json 的全部内容。 我不明白需要调整哪些部分以允许构建多个可分发目录,同时仍然允许一个简单的开发服务器
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"console": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"i18n": {
"sourceLocale": "en-US",
"locales": {
"es-ES": "src/assets/i18n/messages.es-ES.xlf"
}
},
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"localize": ["en-US" ,"es-ES"],
"customWebpackConfig": {
"path": "./extra-webpack.config.js",
"replaceDuplicatePlugins": true
},
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets",
"src/favicon.ico"
],
"styles": [
"node_modules/c3/c3.min.css",
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"es-ES": {
"outputPath": "dist/es-ES/",
"i18nLocale": "es-ES",
"i18nFile": "src/assets/i18n/messages.es-ES.xlf",
"baseHref": "/es-ES/"
},
"en-US": {
"outputPath": "dist/en-US/",
"baseHref": "/en-US/"
},
"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"
}
]
}
}
},
"serve": {
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js",
"replaceDuplicatePlugins": true
},
"browserTarget": "console:build:en-US"
},
"configurations": {
"es-ES": {
"browserTarget": "console:build:es-ES"
},
"production": {
"browserTarget": "console:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "console:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"scripts": [],
"styles": [
"src/styles.scss"
],
"assets": [
"src/assets",
"src/favicon.ico"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"console-e2e": {
"root": "e2e",
"sourceRoot": "e2e",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "console:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"e2e/tsconfig.e2e.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "console",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"style": "scss"
},
"@schematics/angular:directive": {
"prefix": "app"
}
},
"cli": {
"analytics": "62782045-42a1-4d0f-b379-e9a856db628d"
}
}
Angular 的文档说问题是因为您指定了多个本地化并且在开发过程中没有提供覆盖。 在projects > console > architect > build
下添加"development": { "localize": ["en-US"], ...}
以允许您构建多个本地化版本,同时也能够提供服务。 很酷的是你可以用你的其他本地化换掉“en-US”来测试它。 :)
由于 i18n 的部署复杂性和最小化重建时间的需要,开发服务器一次只支持本地化一个语言环境。 如果将“localize”选项设置为 true,定义多个语言环境,并使用 ng serve; 然后发生错误。 如果要针对特定区域进行开发,请将“本地化”选项设置为特定区域。 例如,对于法语 (fr),指定“本地化”:[“fr”]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.