我使用 ionic 2 并浏览了这个文档:
http://ionicframework.com/docs/resources/ng2-translate/
我进行了翻译工作所需的所有更改。但是,当我在 html 文件中写这个时:
{{ 'HELLO' | translate }}
在我看到的页面中
HELLO
这个,甚至认为我在/assets/i18n/en.json 有一个文件,其中包含:
{
"HELLO": "Hello there"
}
在 app.ts 中我添加了这些导入:
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { Http } from '@angular/http';
然后在导入之后我有这个:
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
还有这个在@NgModule->import 的数组中:
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
})
我意识到我的应用在这次导入之前有:
import { HttpModule } from '@angular/http';
我不确定为什么要导入它,但如果我删除它,那么应用程序会在其他地方中断。 那是问题所在吗?使用两个不同的名称两次导入 angular/http? 试图只使用原始的,但在导出函数中它不喜欢参数
http: HttpModule
代替文档中的那个
http: Http
我可以看看 en.json 是否已经加载了吗?或者这里有什么问题?
最佳答案
您可以使用 Inspector Web (F12) 查看 en.json。您是否在代码源中的某处使用了这些方法?
this.translate.setDefaultLang('en');
this.translate.use('en');
在 View 中,您只会看到“HELLO”,因为(我认为是)您没有配置要使用的语言。
关于angular - ionic Angular 平移不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666925/