Angular 11 ngx-translate 不适用于 app.component 以外的其他组件

标签 angular ngx-translate

我正在尝试翻译我的应用程序以使其支持英语和西类牙语。 但它只翻译 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

但是如果我将相同的 html 放在 home.component.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 中

最佳答案

您必须在使用翻译管道的组件所在的模块中导入 TranslateModule

关于Angular 11 ngx-translate 不适用于 app.component 以外的其他组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67461585/

相关文章:

angular - ngx-翻译和 *ngFor

javascript - 使用环境变量动态更改 Angular 6 中的样式

angular - 类型上不存在属性发出

angular - 在 Angular2 中,mailto 链接被识别为组件路由

ngx-translate 页面加载时短暂延迟

angular - ngx-translate 特殊字符问题

angular - ngModel 但使用响应式(Reactive)验证方法

javascript - Injector 中提供的 Angular 6 通用服务需要另一个应用程序注入(inject)变量

angular - ngx-translate 如何测试组件

angular - ionic2 将 ngx-translate 应用于菜单项