如何解决使用ngx-translate
**您好,我安装了ngx-translate,然后在app.component.ts中添加了import { defaultLanguage } from './../assets/i18n/en.json';
,错误是:
“找不到模块'./../assets/i18n/en.json'。考虑使用'--resolveJsonModule'导入扩展名为'.json'的模块”, 但是我有使用该名称的文件夹和文件。需要做什么?我是Angular的初学者。
解决方法
无论您是初学者还是无关紧要的人,因为一个人是外部Angular库,而不是Core Angular特定主题。
如果阅读文档,则说明您正在通过http请求下载json文件并进行加载。因此,您必须拥有一个可以存储它们的服务器。
我与您分享了如何在没有任何http请求的情况下“注入” i18n。因此它将嵌入您的源代码中。
// @ symbol mean is this folder not angular module. So it is just natural folder.
import { I18N_JAPAN } from './@i18n/jp';
import { I18N_ENGLISH } from './@i18n/en';
@NgModule({
declarations: [
AppComponent
],imports: [
// ...
TranslateModule.forRoot(),// <-- need
// ...
],providers: [ /* ... */],bootstrap: [AppComponent],})
export class AppModule {
constructor(translateService: TranslateService) {
translateService.setTranslation('jp',I18N_JAPAN); <-- you can add directly
translateService.setTranslation('en',I18N_ENGLISH); <-- you can add directly
translateService.setDefaultLang('en');
translateService.use(translateService.getBrowserLang());
}
}
export const I18N_ENGLISH = {
'my-i18n': {
path: {
to: 'display text'
}
}
}
并使用:
- 在html中:
{{ 'my-i18n.path.to' | translate }}
- 在TS中:
translateService.instant('my-i18n.path.to')
在文档中:https://github.com/ngx-translate/core
配置
默认情况下,没有可用的加载器。您可以添加翻译 手动使用setTranslation,但最好使用加载程序。您 可以编写自己的加载程序,或导入现有的加载程序。例如你 可以使用TranslateHttpLoader从文件中加载翻译 使用HttpClient。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。