To implement localization in an Angular application using JSON files, you can follow these steps:
Step 1: Set up your Angular project Create a new Angular project using the Angular CLI by running the following command in your terminal:
arduinong new localization-app
Step 2: Install the required dependencies Change your current directory to the project's root folder:
bashcd localization-app
Then, install the ngx-translate package, which will help us with the localization functionality:
bashnpm install @ngx-translate/core @ngx-translate/http-loader
Step 3: Configure the translation module In your app.module.ts file, import the necessary modules and configure the translation module:
typescriptimport { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
defaultLanguage: 'en', // Set the default language
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
declarations: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 4: Create localization files
Create a folder called assets
in the root directory of your project. Inside the assets
folder, create another folder called i18n
. This is where we'll store the localization JSON files.
For example, create the following JSON files:
assets/i18n/en.json
(for English translations)assets/i18n/fr.json
(for French translations)
The JSON files should have key-value pairs representing the translations. For example, in en.json
:
json{
"greeting": "Hello",
"farewell": "Goodbye"
}
Step 5: Add translation service to a component In a component where you want to use localization, import the necessary modules and inject the translation service:
typescriptimport { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-example',
template: `
<h1>{{ 'greeting' | translate }}</h1>
<h2>{{ 'farewell' | translate }}</h2>
`
})
export class ExampleComponent {
constructor(private translate: TranslateService) {
translate.setDefaultLang('en'); // Set the default language
translate.use('en'); // Use the specified language
}
}
Step 6: Use translation pipe in the template
In your component's template, use the translation pipe (translate
) to display the localized content:
html<h1>{{ 'greeting' | translate }}</h1>
<h2>{{ 'farewell' | translate }}</h2>
Step 7: Test the application Start the development server by running the command:
ng serve
Open your browser and navigate to http://localhost:4200
to see your application running with localization support.
You can also switch languages dynamically by calling the translate.use()
method with the desired language code.
That's it! You have now implemented localization in your Angular application using JSON files