[英]Is there a way to use i18n in .pug files in Angular 2
I have tried to add i18n in Angular 2 project in .pug files. 我试图在.pug文件中的Angular 2项目中添加i18n。 The "./node_modules/.bin/ng-xi18n" command fails with "can't resolve module @angular/core/src/di/metadata from "..\\index.ts"".
“./node_modules/.bin/ng-xi18n”命令失败,“无法解析模块@ angular / core / src / di / metadata from”.. \\ index.ts“”。 Is there a way to use i18n localization and .pug or the html is the only solution for now?
有没有办法使用i18n本地化和.pug或html是现在唯一的解决方案? Thanks.
谢谢。
Yes, using angular/cli + ngx-translate + pug-cli will do the work. 是的,使用angular / cli + ngx-translate + pug -cli将完成工作。
First, set up your TranslationConfigModule . 首先,设置您的TranslationConfigModule 。 Create a new module called translation.config.module.ts
创建一个名为translation.config.module.ts的新模块
import { HttpModule, Http } from '@angular/http';
import { NgModule, ModuleWithProviders } from '@angular/core';
import { TranslateModule, TranslateLoader, TranslateService } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
export function HttpLoaderFactory(http: Http) {
return new TranslateHttpLoader(http, '../../../assets/i18n/', '.json');
}
const translationOptions = {
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [Http]
}
};
@NgModule({
imports: [TranslateModule.forRoot(translationOptions)],
exports: [TranslateModule],
providers: [TranslateService]
})
export class TranslationConfigModule {
/**
* @param translate {TranslateService}
*/
constructor(private translate: TranslateService) {
// Setting up Translations
translate.addLangs(['en', 'it']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
// English and Italian, just an example
translate.use(browserLang.match(/en|it/) ? browserLang : 'it');
}
}
Then, import it in whatever module you want,eg app.module.ts
然后,以您想要的任何模块导入它,例如
app.module.ts
. . .
import { TranslationConfigModule } from '../shared/modules/translation.config.module';
. . .
@NgModule({
declarations: [ ],
imports: [
TranslationConfigModule
],
exports: [ ]
})
Now, you need to build some i18n.json
s in myapp/src/assets/i18n
. 现在,你需要在
myapp/src/assets/i18n
构建一些i18n.json
。 EG en.json
that will contain: EG
en.json
将包含:
{
"TEST" : {
"TITLE" : "Herro Pug!"
}
}
Now set up pug integration following the steps on this answer of mine Use Jade as angular2 template engine . 现在按照我的答案中的步骤设置pug集成使用Jade作为angular2模板引擎 。
Now enjoy your pug templates with i18n. 现在用i18n享受你的哈巴狗模板。 Of course you can use this translation module only on the components declared and exported in a module where you are importing this one.
当然,只能在要导入此模块的模块中声明和导出的组件上使用此转换模块。
test.pug test.pug
h3 {{ 'TEST.TITLE' | translate }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.