我正在尝试翻译我的应用程序,使其在英语和西班牙语中都能工作。但它只翻译app.component.html中的内容。
如果我试图翻译任何其他组件中的任何东西,我会得到错误No pipe found with name 'translate'.
这是我的app.module.ts:
...other imports
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
//import { WOW } from 'wow.js';
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
declarations: [
AppComponent,
SecureComponent,
TopNavbarComponent,
FooterComponent
],
imports: [
BrowserModule,
AppRoutingModule,
PublicModule,
RouterModule,
MDBBootstrapModule.forRoot(),
NgwWowModule,
ChartsModule,
NgxIbanModule,
ReactiveFormsModule,
HttpClientModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在app-routing.module.ts中,我这样做:
@NgModule({
imports: [RouterModule.forRoot(routes),
TranslateModule
],
exports: [RouterModule,
TranslateModule]
})
这是我的app.component.ts:
import { Component, OnInit } from '@angular/core';
import { NgwWowService } from 'ngx-wow';
import {TranslateService} from '@ngx-translate/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
constructor(private wowService: NgwWowService, private translate: TranslateService) {
this.wowService.init();
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.setDefaultLang('en');
// for default language to be english, you need to use below code
//translate.use('en');
}
所以如果我在我的app.component.html中这样做:
<div>
<h1>{{ 'home.title' | translate }}</h1>
<!-- translation: translation pipe -->
<p>{{ 'home.text' | translate }}</p>
<!-- translation: directive (key as attribute)-->
<p [translate]="'home.text'"></p>
<!-- translation: directive (key as content of element) -->
<p translate>home.text</p>
</div>
它输出以下内容:
Translation demo
This is a simple demonstration app for ngx-translate
This is a simple demonstration app for ngx-translate
This is a simple demonstration app for ngx-translate
但是如果我在home.component.html中放入相同的html,我会得到错误:
Error: src/app/public/home/home.component.html:4:25 - error NG8004: No pipe found with name 'translate'.
4 <h1>{{ 'home.title' | translate }}</h1>
~~~~~~~~~
src/app/public/home/home.component.ts:6:16
6 templateUrl: './home.component.html',
~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component HomeComponent.
这是我的home.component.ts:
import { Component, OnInit } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(private translate: TranslateService) {
}
ngOnInit(): void {
}
}
我不明白为什么它不能工作,因为所有相关的导入对于app.component和home.component都是相同的。翻译文本位于assets/i18n/en.json和assets/i18n/es.json中
发布于 2021-05-10 03:31:21
您必须在使用translate TranslateModule的组件所在的模块中导入管道
https://stackoverflow.com/questions/67461585
复制相似问题