<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Aniket Potdar</title>
    <description>The latest articles on Forem by Aniket Potdar (@aniket_potdar).</description>
    <link>https://forem.com/aniket_potdar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1926841%2F4e5168f0-8509-49fb-b31b-867a623652bb.png</url>
      <title>Forem: Aniket Potdar</title>
      <link>https://forem.com/aniket_potdar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aniket_potdar"/>
    <language>en</language>
    <item>
      <title>Internationalization (i18n) in Angular</title>
      <dc:creator>Aniket Potdar</dc:creator>
      <pubDate>Wed, 14 Aug 2024 06:49:31 +0000</pubDate>
      <link>https://forem.com/aniket_potdar/internationalization-i18n-in-angular-14da</link>
      <guid>https://forem.com/aniket_potdar/internationalization-i18n-in-angular-14da</guid>
      <description>&lt;p&gt;In today’s expansive digital world, web developers aim to connect with a global audience. A key strategy to achieve this is to transform your Angular application into a multilingual experience. Welcome to Internationalization (i18n), where your app can communicate with users in their native languages, no matter where they are. In this blog, we’ll delve into integrating i18n into your Angular project, ensuring your app is accessible and user-friendly for people across the globe.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Internationalization (i18n) in Angular
&lt;/h2&gt;

&lt;p&gt;Imagine your web app as a bridge connecting users from around the globe. To create a welcoming and user-friendly experience for all, it’s essential to communicate in their language. Internationalization (i18n) is the key to making your app adaptable to various languages and regions. Angular offers robust tools and features to help you accomplish this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up a New Angular Project
&lt;/h2&gt;

&lt;p&gt;Before we begin our i18n journey, let’s create a new Angular project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng new i18n-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the following command to begin a new Angular project using the Angular CLI. During the setup process, you can configure your project, including options such as enabling Angular routing and choosing a stylesheet format (e.g., CSS, SCSS). Make sure to select the settings that match your project's requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing and Configuring Angular’s i18n Tools
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;To kickstart our i18n journey, we’ll need the right tools. We’ll install and set up Angular’s i18n tools to make our app multilingual.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ngx-translate/core --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ngx-translate/http-loader --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Once ngx-translate is installed, we can import it into our Angular module or Config and set up the translation service:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { AppComponent } from './app.component';

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

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using the &lt;strong&gt;app.config&lt;/strong&gt; file, simply replace NgModule with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { ApplicationConfig, importProvidersFrom } from '@angular/core';

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

export const appConfig: ApplicationConfig = {
    providers: [
        importProvidersFrom([
            BrowserModule,
            HttpClientModule,
            TranslateModule.forRoot({
                loader: {
                    provide: TranslateLoader,
                    useFactory: HttpLoaderFactory,
                    deps: [HttpClient],
                },
            }),
        ])
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we import the necessary ngx-translate modules and configure the translation loader using the HttpLoaderFactory function. The HttpLoaderFactory function uses the HttpClient module to load translation files asynchronously from the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Translation Files
&lt;/h2&gt;

&lt;p&gt;Once the translation service is set up, we can create translation files for each language we want to support. Translation files are JSON files that map keys to translated strings.&lt;/p&gt;

&lt;p&gt;Create files with the necessary language code as the name and add translations to them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example, here’s an English translation file:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "title": "My App",
  "greeting": "Hello, {{name}}!"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;And here’s a German translation file:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "title": "Meine App",
  "greeting": "Hallo, {{name}}!"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the folder structure and the names of the files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl5r0cahw80xk70slrlh2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl5r0cahw80xk70slrlh2.PNG" alt="i18n folder structure" width="328" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The translation files utilize the same keys but with different values for each language. This simplifies switching between languages in our application without modifying the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Translations in Templates
&lt;/h2&gt;

&lt;p&gt;With our translation files ready, we can now integrate them into our Angular templates. The first step is to inject the translation service into our component:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.component.ts :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(private translate: TranslateService) {
    // Set the default language
    translate.setDefaultLang('EN');
  }

  translateLanguage(lang: string) {
    // Set the current language
    this.translate.use(lang);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we inject the TranslateService into our component and set English as the default language. We also define a translateLanguage method that allows us to change the application's language.&lt;/p&gt;

&lt;p&gt;Create a component For Language Translation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng g c LanguageTranslation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the component, we can utilize the Translation Service to translate between multiple languages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LanguageTranslation.component.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-language-translator'
})

export class LanguageTranslationComponent {
constructor(private translate: TranslateService) {}

TranslateToEnglish() {
  this.translate.use('En');
}

TranslateToGerman() {
  this.translate.use('DE');
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;LanguageTranslation.component.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;{{ "title" | translate }}&amp;lt;/p&amp;gt;

&amp;lt;div&amp;gt;
    &amp;lt;button (click)="translateToEnglish()"&amp;gt;English&amp;lt;/button&amp;gt;
    &amp;lt;button (click)="translateToGerman()"&amp;gt;German&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we create a LanguageTranslationComponent that injects the TranslateService and provides two buttons to switch between English and German. When a button is clicked, we call the translateLanguage method on the TranslateService with the appropriate language code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When using this.translate.use(‘DE’), ensure to use the correct language code as it is case-sensitive and ensure the file name is exact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Internationalization is a key element of modern software development, and Angular offers robust tools for creating multilingual applications. With the ngx-translate library, implementing i18n in Angular becomes both simpler and more adaptable.&lt;/p&gt;

&lt;p&gt;In this article, we’ve covered how to use ngx-translate to generate translation files, apply translations in templates, and switch languages within an Angular application. By following these guidelines, you can effortlessly build a multilingual app accessible to users worldwide.&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

&lt;p&gt;Feel free to leave comments if you have any questions&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>internalization</category>
    </item>
  </channel>
</rss>
