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.

multi language

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 > footer > widgets > language

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.

language.component.html

Our Html component for language translate will look like this

<div class="translate_wrapper" [ngClass]="layoutService.isLanguage ? 'active' : ''">
    @if(selectedLanguage) {
        <div class="current_lang">
            <div class="lang">
            <i class="flag-icon flag-icon-{{ selectedLanguage.icon }}"></i>
            <span class="lang-txt">{{ selectedLanguage.code }}</span>
            </div>
        </div>
    }
    <div class="more_lang" [ngClass]="layoutService.isLanguage ? 'active' : ''">
        @for(language of languages; track language) {
            <div class="lang selected" (click)="selectLanguage(language)">
                <i class="flag-icon flag-icon-{{ language.icon }}"></i>
                <span class="lang-txt">{{ language.name }}
                    @if(language.country_code) {
                        <span> ({{ language.country_code }})</span>
                    }
                </span>
            </div>
        }
    </div>
</div>
language.component.ts

Language change logic and configuration:

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

import { language } from '../../../../data/header';
import { Language } from '../../../../interface/header';
import { LayoutService } from '../../../../services/layout.service';

@Component({
    selector: 'app-language',
    imports: [CommonModule],
    templateUrl: './language.component.html',
    styleUrl: './language.component.scss'
})

export class LanguageComponent {

    public languages = language;
    public selectedLanguage: Language;

    constructor(public layoutService: LayoutService, public translate: TranslateService) {
    this.languages.filter((details) => {
        if (details.active) {
        this.selectedLanguage = details
        }
    })

    this.translate.use('en');
    }

    selectLanguage(language: Language) {
    this.selectedLanguage = language;
    this.translate.use(language.code)
    }

}
header.ts

language data:

export const language: Language[] = [
    {
        id: 1,
        name: 'English',
        code: 'en',
        icon: 'us',
        country_code: "US",
        active: true
    },
    {
        id: 2,
        name: 'Deutsch',
        code: 'de',
        icon: 'de'
    },
    {
        id: 3,
        name: 'Español',
        code: 'es',
        icon: 'es'
    },
    {
        id: 4,
        name: 'Français',
        code: 'fr',
        icon: 'fr'
    },
    {
        id: 5,
        name: 'Português',
        code: 'pt',
        icon: 'pt',
        country_code: "BR"
    },
    {
        id: 6,
        name: '简体中文',
        code: 'cn',
        icon: 'cn'
    },
    {
        id: 7,
        name: 'لعربية',
        code: 'ae',
        icon: 'ae',
        country_code: "ae"
    }
]

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",
    "dashboards": "Dashboards",
    "default": "Default",
    "ecommerce": "Ecommerce",
    "Widgets": "Widgets",
    "Chart": "Chart",
}
languages JSON for Deutsch Language de.json
{
    "general": "Allgemein",
    "dashboards": "Dashboards",
    "default": "Standard",
    "ecommerce": "E-Commerce",
    "Widgets": "Widgets",
    "Chart": "Diagramm",
}
languages JSON for Spanish Language es.json
{
    "general": "General",
    "dashboards": "Tableros",
    "default": "Predeterminado",
    "ecommerce": "Comercio electrónico",
    "Widgets": "Widgets",
    "Chart": "Gráfico",
}
languages JSON for French Language fr.json
{
    "general": "Général",
    "dashboards": "Tableaux de bord",
    "default": "Par défaut",
    "ecommerce": "Ecommerce",
    "Widgets": "Widgets",
    "Chart": "Graphique",
}
languages JSON for Portuguese Language pt.json
{
    "general": "Geral",
    "dashboards": "Painéis de Controle",
    "default": "Padrão",
    "ecommerce": "Ecommerce",
    "Widgets": "Widgets",
    "Chart": "Gráfico",
}
languages JSON for Chinese Language cn.json
{
    "general": "常规",
    "dashboards": "仪表盘",
    "default": "默认",
    "ecommerce": "电子商务",
    "Widgets": "小部件",
    "Chart": "图表",
}
languages JSON for Arabic Language ae.json
{
    "general": "عام",
    "dashboards": "لوحات القيادة",
    "default": "افتراضي",
    "ecommerce": "التجارة الإلكترونية",
    "Widgets": "الأدوات",
    "Chart": "الرسم البياني",
}

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

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


import { ApplicationConfig } from '@angular/core';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { provideAnimations } from '@angular/platform-browser/animations'
import { routes } from './app.routes';
import { HttpClient, provideHttpClient } from '@angular/common/http';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

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

export const appConfig: ApplicationConfig = {
    providers: [
        provideAnimations(),
        provideHttpClient(),
        importProvidersFrom(
            TranslateModule.forRoot({
                loader: {
                    provide: TranslateLoader,
                    useFactory: HttpLoaderFactory,
                    deps: [HttpClient],
                },
            }),
        )
    ]
};

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.