1
votes

I have a working ngx translate in my angular project. In my normal components they have a .module.ts files that ı can put my translateModule into declerations array, but I have a directive which doesnt have a module.ts so my directive cant realize my ngx translate. so how can I fix this issue? my versions: "@ngx-translate/core": "^13.0.0", "@ngx-translate/http-loader": "^6.0.0",

angular 9 ionic angular 5

in my html: {{'weather' | translate}} but my directive cant recognize it

my app.module.ts:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientModule } from '@angular/common/http';
import { MatIconModule } from '@angular/material/icon';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CommonModule } from '@angular/common';
import { HTTP } from '@ionic-native/http/ngx';
import { NativeStorage } from '@ionic-native/native-storage/ngx';
import { IonicStorageModule } from '@ionic/storage';
import { HttpService } from './services/http.service';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import {MatInputModule} from '@angular/material/input';
import {MatCardModule} from '@angular/material/card';
import {MatSelectModule} from '@angular/material/select';
import {MatDialogModule} from '@angular/material/dialog';



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

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

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  schemas:[CUSTOM_ELEMENTS_SCHEMA],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    HttpClientModule,
    MatIconModule,
    IonicStorageModule.forRoot(),
    FormsModule,
    CommonModule,
    BrowserAnimationsModule,
    MatInputModule,
    MatCardModule,
    MatSelectModule,
    MatDialogModule,
    ReactiveFormsModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (HttpLoaderFactory),
        deps: [HttpClient]
      }
    })

  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    HTTP,
    Geolocation,
    NativeStorage,
    HttpService,
    MatDialogModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

for example, one of my normal working components module with translate:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { DashboardPageRoutingModule } from './dashboard-routing.module';

import { DashboardPage } from './dashboard.page';
import { ToolbarCustomComponent } from 'src/app/directives/toolbar-custom/toolbar-custom.component';
import { TranslateModule } from '@ngx-translate/core';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    DashboardPageRoutingModule,
    TranslateModule
  ],
  declarations: [DashboardPage, ToolbarCustomComponent]
})
export class DashboardPageModule {}
1
I am having the same issue were you able to find a solutioniqueqiorio
Yes I fixed it ı will share my solution as a new comment @iqueqiorioWorldOfEmre

1 Answers

0
votes

I created a sharedModule.ts file here: (you dont need to import this file to somewhere or do any extra thing, thats all)

enter image description here

and codes of sharedModule.ts :

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';  
import { ToolbarCustomComponent } from './directives/toolbar-custom/toolbar-custom.component';
import { TranslateModule } from '@ngx-translate/core';

@NgModule({
    imports: [
        CommonModule,
        TranslateModule  
    ],
    declarations: [
        ToolbarCustomComponent  //your custom component
    ],
    exports: [
        ToolbarCustomComponent  //your custom component
    ]
})
export class SharedModule {}

in your html:

{{'dashboardPage.yourReservations' | translate}}