Multi-Language
Support
Considering that there will be users from multiple different countries, you might need to add the support for multiple help you with that, we have made the template 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 >> 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.
languages.components.html
Our Html component for language translate will look like this
<div *ngIf="style == 'basic'" (clickOutside)="hideDropdown()">
<app-button
[class]="'btn dropdown-toggle'"
[type]="'button'"
[id]="'open_dropdown_basic_btn'"
[spinner]="false"
(click)="openDropDown()">
<span><div class="iti-flag {{ selectedLanguage.icon }}"></div> {{ selectedLanguage.language }}</span>
</app-button>
<ul class="dropdown-menu dropdown-menu-end language-dropdown" [class.show]="active" >
<li *ngFor="let language of languages">
<a class="dropdown-item" href="javascript:void(0)" (click)="selectLanguage(language)">
<div class="iti-flag {{ language.icon }}"></div>
<span>{{ language.language }}</span>
</a>
</li>
</ul>
languages.components.ts
Language change logic and configuration:
import { Component, Input } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { languages } from '../../../../../shared/interface/theme-option.interface';
@Component({
selector: 'app-language',
templateUrl: './language.component.html',
styleUrls: ['./language.component.scss']
})
export class LanguageComponent {
@Input() style: string = 'basic';
public active: boolean = false;
public languages: languages[] = [
{
language: 'English',
code: 'en',
icon: 'us'
},
{
language: 'Français',
code: 'fr',
icon: 'fr'
},
]
public selectedLanguage: languages = {
language: 'English',
code: 'en',
icon: 'us'
}
constructor(private translate: TranslateService) {
this.selectLanguage(this.selectedLanguage)
}
selectLanguage(language: languages){
this.active = false;
this.translate.use(language.code);
this.selectedLanguage = language;
}
openDropDown(){
this.active = !this.active;
}
hideDropdown(){
this.active = false;
}
}
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 en.json
{
"collections": "Collections",
"collection_layouts": "Collection Layouts",
"collection_left_sidebar": "Collection Left Sidebar",
"collection_right_sidebar": "Collection Right Sidebar",
"collection_no_sidebar": "Collection No Sidebar"
}
languages JSON for spanish Language fr.json
{
"collections": "Collections",
"collection_layouts": "Dispositions des collections",
"collection_left_sidebar": "Collection restante Barre latérale",
"collection_right_sidebar": "Barre latérale droite de la collection",
"collection_no_sidebar": "Collection sans barre latérale",
}
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.