javascriptangulartypescriptngx-translate

Angular 12 ngx-translate / loader isn't working


https://www.npmjs.com/package/@ngx-translate/core https://www.npmjs.com/package/@ngx-translate/http-loader I installed translate package from here and added ngx-translate module with its forRoot , translate service inside component ts (app & header) but it isn't working also

Header component

<header>
<nav class="navbar navbar-expand-lg navbar-light" id="header">
    <div class="container">
      
      <a class="navbar-brand" href="#">
        <img src=".." alt="logo">
      </a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{ exact: true }">
           
            <a [routerLink]="['home']" class="nav-link">Home</a>

          </li> 
        
          <li class="nav-item dropdown" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{ exact: true }">
            <a class="nav-link dropdown-toggle"  id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              page 2
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
              <li><a class="dropdown-item" [routerLink]="['page']">page</a></li>
            </ul>
          </li>

          <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{ exact: true }">
            <a class="nav-link" [routerLink]="['features']">Features</a>
          </li>
  
          <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{ exact: true }">
            <a class="nav-link reserve-btn" [routerLink]="['reserve']">Reserve</a>
          </li>

        </ul>
     
      </div>
<button (click)="translate.use('en')">
  En
</button>

<button (click)="translate.use('ar')">
  Ar
</button>
      
  </div>
  </nav>
</header>

Header.component.ts

import { DOCUMENT } from '@angular/common';
import { Component, HostListener, Inject, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
})

export class HeaderComponent implements OnInit {
  // lang: string;

  constructor(@Inject(DOCUMENT) private document: Document,public translate:TranslateService) {}
 
  
  ngOnInit(): void {
    
    // this.lang = localStorage.getItem('lang') || 'en';
  }


}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { GeneralPagesModule } from './general-pages/general-pages.module';
import { RentalPageModule } from './rental-page/rental-page.module';
import { SharedModule } from './shared/shared.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

import { HttpClientModule, HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';

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

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    SharedModule,
    GeneralPagesModule,
    RentalPageModule,
    BrowserAnimationsModule,
    // TranslateModule,
     HttpClientModule,
    TranslateModule.forRoot({
      defaultLanguage:'en',
      loader: {
        provide: TranslateLoader,
        useFactory: createTranslateLoader,
        deps: [HttpClient]
      }
    }),
  ],
  providers: [
    // { provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true },

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

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

I also tried same steps in app.component.ts

import { Component, Inject } from '@angular/core';
import * as AOS from 'aos';
import { DOCUMENT } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { filter} from 'rxjs/operators';



@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  ngOnInit(){
    AOS.init({
      duration: 1200,
    });
    }


  // Inject document which is safe when used with server-side rendering
  constructor(@Inject(DOCUMENT) private document: Document,public translate:TranslateService,
  private router: Router, private route: ActivatedRoute) {

   
  }


}

in home.compoent.html

{{"HOME" || translate}}

home.compoent.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(public translate:TranslateService) { }

  ngOnInit(): void {
  }

}

Solution

  • The pipe syntax is {{ 'my-translation-key' | translate }} - you have double || in your template, please change and try again.

    Alternatively you can inject the translate service and use myTanslation = this.translateService.instant('my-translation-key') if you need to set a variable in your controller.