如何在指令中使用 ngx 翻译

How to use ngx translate in a directive

我的 angular 项目中有一个可用的 ngx 翻译。在我的普通组件中,它们有一个 .module.ts 文件,我可以将我的 translateModule 放入 declerations 数组,但我有一个没有 module.ts 的指令,所以我的指令无法实现我的 ngx 翻译。那么我该如何解决这个问题呢? 我的版本: "@ngx-translate/core": "^13.0.0", "@ngx-translate/http-loader": "^6.0.0",

angular 9 离子 angular 5

在我的 html 中:{{'weather' | translate}} 但我的指令无法识别它

我的应用程序。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 { }

例如,我的正常工作组件模块之一带有翻译:

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 {}

我在这里创建了一个 sharedModule.ts 文件:(你不需要将这个文件导入到某个地方或做任何额外的事情,仅此而已)

和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 {}

在你的html中:

{{'dashboardPage.yourReservations' | translate}}