Ionic3 The pipe 'translate' could not be found
Asked Answered
F

1

10

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');
     ...
}
Freed answered 27/6, 2018 at 8:25 Comment(0)
J
15

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
],
Jenninejennings answered 27/6, 2018 at 8:42 Comment(2)
Great! Glad to hear, have a nice day and happy coding! :) :)Jenninejennings
@Kapilsoni, sorry, when not seeing the code, it's hard to help. You can consider posting a new question where you show what you have attempted, if this solution doesn't work, there might be something else going on. So if you ask a new question, make sure it's a minimal reproducible example for best chances of getting help :)Jenninejennings

© 2022 - 2024 — McMap. All rights reserved.