迁移到 angular v9。 i18n 重大改变

Migration to the angular v9. i18n breaking changes

我正在将 Angular 项目从 8.2.14 迁移到 9.0.0-rc.14。使用 i18n 进行本地化。

我按照这里的指南https://update.angular.io/#8.2:9.0 感觉还不错。升级后,我正在尝试 运行 项目,但是

ng serve 导致以下错误:

An unhandled exception occurred: An i18n locale ('en-US') cannot both be a source locale and provide a translation.
See "...my local logs path...\angular-errors.log" for further details.

angular-errors.log中错误更详细:

[error] Error: An i18n locale ('en-US') cannot both be a source locale and provide a translation.
    at createI18nOptions (...my project path...\node_modules\@angular-devkit\build-angular\src\utils\i18n-options.js:69:23)
    at Object.configureI18nBuild (...my project path...\node_modules\@angular-devkit\build-angular\src\utils\i18n-options.js:100:18)
    at process._tickCallback (internal/process/next_tick.js:68:7)

我得到了有关该错误的更多详细信息:打开 @angular-devkit\build-angular\src\utils\i18n-options.js:69:23,我看到了以下逻辑:

if (locale === i18n.sourceLocale) {
    throw new Error(`An i18n locale ('${locale}') cannot both be a source locale and provide a translation.`);
}

我有 en-US 语言环境,我想继续将其用作翻译和源代码。那么如何修复这个破坏性错误呢?

只需打开您的 angular.json 文件并删除您在 "sourceLocale" 下设置的语言环境下的条目。更新区域设置后仅存储翻译而不是您在源代码中使用的语言。

我假设您的默认语言不是英语。 因此,您应该更改默认源 'sourceLocale',如 official docs.

中所示

所以它会像:

"projects": {
    "AngularApp": {
        "i18n": {
            "sourceLocale": "he-IL",
            "locales": {
                "en-US": "src/locale/messages.en.xlf"
            }
        },
...

正如@Markus 所说:

您的原始语言(开发者)不得出现在'"locales":'。

仅在'"sourceLocale":'.