Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic3 The pipe 'translate' could not be found

hi it's been days since I've tried to solve this problem without success. When I try to use the pipe translate i get this error

Error: Uncaught (in promise): Error: Template parse errors:
The pipe 'translate' could not be found ("
<ion-content padding>
<h2>{{[ERROR ->]"HELLO" | translate }}</h2>
</ion-content>
"): ng:///AdminPannelPageModule/AdminPannelPage.html@11:8

I am using angular 5.

these are the versions I use for translation

"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",

Npm install

npm install @ngx-translate/core @ngx-translate/http-loader --save

This is my Home.html page

<h2>{{"HELLO" | translate }}</h2>

JSON file for translate:

assets/i18n/en.json

{
"HELLO": "hello"
}

assets/i18n/it.json

{
"HELLO": "ciao"
}

in the export I used the export function setTranslateLoader (http: HttpClient) instead of using the export function setTranslateLoader (http: Http) because if not it gave me this error: Argument of type 'Http' is not assignable to parameter of type 'HttpClient '. Property 'handler' is missing in type 'Http'.

There are my import in app.module.ts

import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

export function setTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}


@NgModule({
imports: [
HttpClientModule,
IonicModule.forRoot(MyApp),
TranslateModule.forRoot({
  loader: {
    provide: TranslateLoader,
    useFactory: (setTranslateLoader),
    deps: [HttpClient]
  }
}),
],

And This is my app.component.ts constructor

constructor(..., translate: TranslateService) {
     translate.setDefaultLang('en');
     ...
}
like image 960
alessandro cignolini Avatar asked Jun 27 '18 08:06

alessandro cignolini


1 Answers

We can see that you are having a sub module AdminPannelPageModule, where the error is actually thrown. You need to mark TranslateModule in that module as well, but skip forRoot(). So in your AdminPannelModule:

imports: [
  ....
  TranslateModule
],
like image 154
AT82 Avatar answered Oct 30 '22 21:10

AT82