Top

Multi-Language Support


Considering that there will be users from multiple different countries, you might need to add the support for multiple languages.To help you with that, we have made the sidebar compatible with the multiple language functionality.

You can find the dropdown that changes the language in the header.

How does multi-language functionality works?

We have used the packages @ngx-translate/core and @ngx-translate/http-loader , you can install it by running the following commands in the terminal.

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

You will find language translator logic and design at below given path

src > app > shared > components > header > elements > languages

Implementing Language Translate Feature

After you finished installing above mentioned packages in your project, follow the below given steps to add the multi-language support.

languagess.html

Our Html component for language translate will look like this

<div class="translate_wrapper" [class.active]="navService.language">
  <div class="current_lang">
    <div class="lang">
       <i class="flag-icon flag-icon-{{ selectedLanguage.icon }}"></i><span class="lang-txt">{{ selectedLanguage.code }}</span>
      <svg>
        <use href="assets/svg/icon-sprite.svg#arrow-down-fill"> </use>
      </svg>
    </div>
  </div>
  <div class="more_lang" [class.active]="navService.language">
    @for(details of languages; track details){
      <div class="lang selected" (click)="changeLanguage(details)">
        <i class="flag-icon flag-icon-{{ details.icon }}"></i>
        <span class="lang-txt">{{ details.language }}<span>
          @if(details.type){
            ({{ details.type }})
          }
        </span>
      </span>
    </div>
    }
  </div>
</div>
languages.ts

Language change logic and configuration:

import { Component } from '@angular/core';
import { NavService } from '../../../../services/nav.service';
import { languages } from '../../../../data/data/header';
import { language } from '../../../../interface/header';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-header-language',
  imports: [],
  templateUrl: './header-language.html',
  styleUrl: './header-language.scss'
})
export class HeaderLanguage {

  public languages = languages;
  public selectedLanguage: language;
  public navServices: NavService = inject(NavService);
  private translate: TranslateService = inject(TranslateService);

  constructor(){
    this.languages.filter((data) => {
      if(data.active){
        this.selectedLanguage = data
      }
    })
  }

  changeLanguage(item: language){
    this.selectedLanguage = item;
    this.translate.use(item.code)
  }
}
header.ts
export interface language{
  language: string;
  code: string;
  type?: string;
  icon: string;
  active?: boolean;
}

export const languages: language[] = [
  {
    language: 'English',
    code: 'en',
    type: 'US',
    icon: 'us',
    active: true
  },
  {
    language: 'Español',
    code: 'es',
    icon: 'es'
  },
  {
    language: 'Français',
    code: 'fr',
    icon: 'fr'
  },
  {
    language: 'Português',
    code: 'pt',
    type: 'BR',
    icon: 'pt'
}]
JSON files for different languages

To translate words we have to provide the translation for each word, that we will do in a json file.

languages JSON for English Language en.json
{
   "General": "General",
   "Dashboard": "Dashboard",
   "Crypto": "Crypto",
   "Ecommerce": "Ecommerce",
   "Widgets": "Widgets",
   "Chart": "Chart",
}
languages JSON for Spanish Language es.json
{
   "General": "General",
   "Dashboard": "Panel",
   "Crypto": "Cripto",
   "Ecommerce": "Comercio electrónico",
   "Widgets": "widgets",
   "Chart": "Cuadro",
}
languages JSON for French Language fr.json
{
   "General": "Générale",
   "Dashboard": "Tableau de bord",
   "Crypto": "Cryptomonnaie",
   "Ecommerce": "Commerce électronique",
   "Widgets": "Widget",
   "Chart": "Graphique",
}
languages JSON for Português Language pt.json
{
   "General": "Geral",
   "Dashboard": "painel de controle",
   "Crypto": "Criptografia",
   "Ecommerce": "Comércio eletrônico",
   "Widgets": "Widgets",
   "Chart": "gráfico",
}

Now We just need to configure the settings for language change in our app.module.ts file.

Add the following code in your app.module.ts file.

import { TranslateHttpLoader } from '@ngx-translate/http-loader';
         
export function HttpLoaderFactory(http: HttpClient) {
   return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

@NgModule({
   declarations: [
      ...
   ],
   imports: [
      TranslateModule.forRoot({
      loader: {
         provide: TranslateLoader,
         useFactory: HttpLoaderFactory,
         deps: [HttpClient]
      },
      }),
      ...
   ],
   providers: [...],
   bootstrap: [..]
})
export class AppModule { }

Now we are all set to use language translation, you just have to add | translate when you want a word to change when the language changes.

For Example:

Use a variable as follows:

{{ some_variable | translate }}

Warning: Make sure that if you are using a variable then you have added the translation for every word possible in that variable in the json file.