Ionic i18n for Multilingual Mobile Apps
Q: How do you use Ionic internationalization (i18n) to create a mobile application with support for multiple languages and cultures?
- Ionic
- Mid level question
Explore all the latest Ionic interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Ionic interview for FREE!
Ionic provides support for internationalization (i18n) out of the box. You can use the `ngx-translate` package to translate your app's content to multiple languages. Here are the basic steps to set up internationalization in an Ionic app:
1. Install `ngx-translate` using npm:
npm install @ngx-translate/core --save
2. Create a `translation.json` file in your app's `src/assets/i18n` directory. This file will contain all the translations for your app.
3. Define the translations in the `translation.json` file. For example:
{ "hello": { "en": "Hello", "fr": "Bonjour", "es": "Hola" }, "goodbye": { "en": "Goodbye", "fr": "Au revoir", "es": "Adiós" } }
4. Import the `TranslateModule` in your app's `app.module.ts` file:
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { HttpClientModule, HttpClient } from '@angular/common/http'; import { AppComponent } from './app.component'; export function HttpLoaderFactory(http: HttpClient) { return new TranslateHttpLoader(http, './assets/i18n/', '.json'); } ({ imports: [ BrowserModule, HttpClientModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: HttpLoaderFactory, deps: [HttpClient] } }) ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {}
This sets up the translation module with the `TranslateHttpLoader` to load the translation files from the `assets/i18n` directory.
5. Use the `translate` pipe to display the translated content in your templates. For example:
<ion-content> <h1>{{ 'hello' | translate }}</h1> <p>{{ 'goodbye' | translate }}</p> </ion-content>
This will display "Hello" and "Goodbye" in English by default, but will switch to the appropriate translation based on the user's language settings.
You can also use the `TranslateService` to programmatically switch the app's language based on user preferences or other factors.
import { Component } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; ({ selector: 'app-root', template: ` <ion-header> <ion-toolbar> <ion-buttons slot="start"> <ion-button (click)="switchLanguage('en')">English</ion-button> <ion-button (click)="switchLanguage('fr')">Français</ion-button> <ion-button (click)="switchLanguage('es')">Español</ion-button> </ion-buttons> </ion-toolbar> </ion-header> <ion-content> <h1>{{ 'hello' | translate }}</h1> <p>{{ 'goodbye' | translate }}</p> </ion-content> ` }) export class AppComponent { constructor(private translate: TranslateService) { translate.setDefaultLang('en'); } switchLanguage(language: string) { this.translate.use(language); } }
This will display a set of buttons to switch between English, French, and Spanish translations.


