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 > 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.
languages.html
Our Html component for language translate will look like this
<div class="translate_wrapper" [class.active]="navServices.language">
<div class="current_lang">
<div class="lang">
<i class="flag-icon flag-icon-{{selectedLanguage.icon}}"></i>
</div>
</div>
<div class="more_lang" [class.active]="navServices.language">
@for(lang of languages; track lang){
<div class="lang" data-value="en" (click)="changeLanguage(lang)">
<i class="flag-icon flag-icon-{{lang.icon}}"></i>
<span class="lang-txt">{{lang.language}}
@if(lang.type){
<span>({{lang.type}})</span>
}
</span>
</div>
}
</div>
</div>
languages.ts
Language change logic and configuration:
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { NavmenuService } from '../../../../shared/services/navmenu.service';
interface selectedlanguage {
language?: string;
code?: any;
type?: string;
icon?: string;
}
@Component({
selector: 'app-language',
import:[],
templateUrl: './language.html',
styleUrls: ['./language.scss'],
})
export class Language {
public language: boolean = false;
public languages: selectedlanguage[] = [{
language: 'English',
code: 'en',
type: 'US',
icon: 'us'
},
{
language: 'Español',
code: 'es',
icon: 'es'
},
{
language: 'Français',
code: 'fr',
icon: 'fr'
},
{
language: 'Português',
code: 'pt',
type: 'BR',
icon: 'pt'
}]
public selectedLanguage: selectedlanguage = {
language: 'English',
code: 'en',
type: 'US',
icon: 'us'
}
constructor(public navServices: NavmenuService, private translate: TranslateService) { }
changeLanguage(lang: selectedlanguage) {
this.translate.use(lang.code)
this.selectedLanguage = lang;
}
}
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 spanish Language es.json
"Dashboards" : "Paneloj",
"Default": "Por defecto",
"Widget":"widget",
"Apps":"aplicación",
languages JSON for French Language fr.json
"Dashboards": "Tableau de bord",
"Default": "Défaut",
"Widget":"widget",
"Apps":"applications",
languages JSON for Português Language pt.json
"Dashboards": "painel de controle",
"Default": "Padrão",
"Widget":"Ferramenta",
"App":"Aplicativa",
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.