在 Angular 7 中,生成的生产构建(prod build)中找不到 i18n 文件可能是因为没有正确配置和导入 i18n 相关的文件。下面是一种解决方法,包含代码示例:
angular.json 文件中配置了 i18n 相关的配置。找到 projects -> [your-project-name] -> architect -> build -> configurations -> production 部分,确保有以下配置:"i18n": {
"input": "src/locale"
}
在这个例子中,假设 i18n 文件存放在 src/locale 目录下。
src/locale 目录,并在该目录下添加一个 messages.xlf 文件。可以使用 Angular 提供的 ng xi18n 命令来自动生成该文件:ng xi18n --output-path src/locale
这个命令会根据你的项目代码中的标记生成一个 messages.xlf 文件,并将其放置在 src/locale 目录下。
app.module.ts 文件中导入 LOCALE_ID 和 registerLocaleData,并注册适当的语言环境。例如,如果你要使用英文(en)语言环境,可以这样配置:import { NgModule, LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeEn from '@angular/common/locales/en';
registerLocaleData(localeEn);
@NgModule({
// ...
providers: [{ provide: LOCALE_ID, useValue: 'en' }],
// ...
})
export class AppModule { }
确保根据你的需求导入和注册适当的语言环境。
angular.json 文件中找到 projects -> [your-project-name] -> architect -> build -> options 部分,确保有以下配置:"localize": true
这个配置将确保在构建过程中包含 i18n 文件。
ng build --prod
构建完成后,检查生成的 dist 目录是否包含了 i18n 目录和相关的文件。
通过按照上述步骤配置和生成构建,你应该能够在 Angular 7 的生产构建中找到 i18n 文件。