How can you include locale data in your Angular application?

Leader posted 4 min read

Using locale data in your Angular application offers several important benefits, especially when building apps for a global audience.


Benefits of Using Locale Data in Angular

1. Internationalization (i18n) Support

  • Locale data allows your app to display content in different languages and formats based on the user's region.
  • Enables dynamic language switching, improving accessibility and usability for global users.

2. Localized Date, Time, and Currency Formats

  • Angular's DatePipe, CurrencyPipe, and DecimalPipe automatically format values according to the active locale.
  • Example:

    • en-US: 12/31/2025
    • fr-FR: 31/12/2025

3. Currency Symbols and Number Formatting

  • Numbers and currencies are formatted based on regional conventions (e.g., decimal vs comma separators).
  • Avoids confusion and improves the trust and readability for financial apps.

4. Pluralization Rules

  • Supports grammatically correct plural forms depending on the locale.
  • Useful in strings like “1 item” vs “2 items” — which varies in structure across languages.

5. Built-in Angular i18n Tools Compatibility

  • Locale data is necessary for using Angular's built-in i18n features (e.g., @angular/localize).
  • Helps with ahead-of-time (AOT) translation file compilation.

6. Better User Experience

  • Users are more comfortable and confident using an app that behaves naturally in their language and cultural context.

Example: Registering Locale in Angular

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

registerLocaleData(localeFr);

Then in your providers:

{ provide: LOCALE_ID, useValue: 'fr' }

Complete setup:

To include locale data in your Angular application and enable localization, Run this command:

 `ng build --localize` 

Step 1: Set Up Your Angular Project for Localization

  1. Install Angular Localize Package:
    Ensure you have the Angular Localize package installed. You can add it to your project with:
ng add @angular/localize
  1. Update angular.json:
    In your angular.json file, define the different locales you want to support. Look for the projects section and add your locales under i18n like this:

specific changes:

 "projects": {
    "<your-project-name>": {
      "i18n": {
        "sourceLocale": "en-US",
        "locales": {
           "fr": "src/locale/messages.fr.xlf",
       "es": "src/locale/messages.es.xlf"
        }
      },
      ...
      "architect": {
        "build": {
          ...
          "options": {
            "localize": [
              "en-Us"
            ],
            "aot": true,
            "i18nMissingTranslation": "error",
            "outputPath": "dist/your-project-name",
            ...
          },
          "configurations": {
            "production": {
              ...
            },
            "development": {
              ...
            },
            "en-US": {
              "localize": [
                "en-US"
              ]
            },
            "nl": {
              "localize": [
                "nl"
              ]
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "<your-project-name>:build:production"
            },
            "development": {
              "browserTarget": "<your-project-name>:build:development"
            },
            "en-US": {
              "browserTarget": "<your-project-name>:build:development,en-US"
            },
            "nl": {
              "browserTarget": "<your-project-name>:build:development,nl"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "<your-project-name>:build",
            "format": "xlf",
            "outputPath": "src/locale"
          }
        },
        ...
        "deploy": {
          "builder": "@angular/fire:deploy",
          "options": {
            "prerender": false,
            "ssr": false,
            "browserTarget": "<your-project-name>:build:production",
            "firebaseProject": "<your-project-name>-i18n",
            "firebaseHostingSite": "<your-project-name>-i18n"
          }
        }
      }
    }
  },
Full code: 

  {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "localization-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "i18n": {
        "sourceLocale": "en",
        "locales": {
          "fr": {
            "translation": "./src/locale/messages.fr.xlf",
            "baseHref": ""
          }
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/localization-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "localize": [
              "fr"
            ],
            "aot": true,
            "i18nMissingTranslation": "error",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "localize": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ]
            },
            "fr": {
              "localize": ["fr"]
            },
            "hi": {
              "aot": true,
              "i18nFile": "src/translate/convertApp.hi.xlf",
              "i18nFormat": "xlf",
              "i18nLocale": "hi",
              "i18nMissingTranslation": "error"
            },
            "development": {
              "localize": false,
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": false,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "localization-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "localization-app:build:production"
            },
            "en-US": {
              "browserTarget": "localization-ap:build:development,en-US"
            },
            "fr": {
              "browserTarget": "localization-app:build:development,fr"
            },
            "development": {
              "browserTarget": "localization-app:build:development"
            }
          },
            "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "localization-app:build",
            "format": "xlf",
            "outputPath": "src/locale"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "localization-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "localization-app:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "localization-app",
  "cli": {
    "analytics": "ba193fb2-91a2-4501-a4e7-45943ddfd7b2"
  }
}

Step 2: Create Translation Files

  1. Generate Translation Files:
    Use the Angular CLI to extract messages and create a translation file:
   ng xi18n

This will create a messages.xlf file in the src/locale directory. You can then translate this file into the target languages, renaming it appropriately (e.g., messages.fr.xlf for French).

Step 3: Add Translations

  1. Translate Messages:
    Edit your generated translation files to include the translations for the text used in your application.

Step 4: Build the Application

  1. Build with Localization:
    Once your translation files are ready, you can build your application for a specific locale or all defined locales using:
   ng build --localize

This command will generate separate builds for each locale you defined in your angular.json.

Step 5: Serve the Application

  1. Serve Localized Builds:
    After building, you can serve your application. If you want to test a specific locale, you can serve the localized version by specifying the locale:

    ng serve --configuration=fr
    

Summary

By following these steps, you can include and manage locale data in your Angular application, enabling it to support multiple languages and regions effectively.

0 votes
0 votes

More Posts

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

Discover how learning in public and networking can transform your tech career!

Michael Larocca - May 7, 2025

Understanding Basic Data Structures for Web Development

MasterCraft - Feb 16

Optimizing the Clinical Interface: Data Management for Efficient Medical Outcomes

Huifer - Jan 26

Bridging the Silence: Why Objective Data Outperforms Subjective Health Reports in Elderly Care

Huifer - Jan 27
chevron_left