Globalization in Angular Datetimepicker component
27 Apr 20247 minutes to read
Globalization is the combination of internalization and localization. You can adapt the component to various languages by parsing and formatting the date or number Internationalization
and also add culture specific customization and translation to the text localization
.
By default, the date format, week, month, time format and meridian names are specific to the American English
culture. It utilizes the Essential JavaScript 2 Internationalization
package to parse and format the date object based on the culture by using the official UNICODE CLDR
JSON data. It provides the loadCldr
method to load culture specific CLDR JSON data. To use a different culture other than English
, follow the steps below:
- Install the
CLDR-Data
package by using the following command (installs all the CLDR JSON data). To know more about CLDR-Data refer to theCLDR-Data
link.
npm install cldr-data --save
Once the package installed, you can find the culture specific JSON data under the location /node_modules/cldr-data
.
-
Now import the installed CLDR JSON data into the
app.component.ts
file. -
Now use the
loadCldr
method to load the culture specific CLDR JSON data from the installed location toapp.component.ts
file. -
DateTimePicker displayed
Sunday
as the first day of week based on default culture (“en-US”). If you want to display the DateTimePicker with loaded culture’s first day of week, you need to importweekdata.json
file from thecldr-data/suppemental
as given in the code example.
import { loadCldr } from "@syncfusion/ej2-base";
declare var require: any;
loadCldr(
require("cldr-data/main/de/numbers.json"),
require("cldr-data/main/de/ca-gregorian.json"),
require("cldr-data/supplemental/numberingSystems.json"),
require("cldr-data/main/de/timeZoneNames.json"),
require('cldr-data/supplemental/weekdata.json') // To load the culture based first day of week
);
The
Localization
library allows you to localize default text content of the DateTimePicker. The DateTimePicker component has static text for today feature that can be changed to other cultures (Arabic, Deutsch, French, etc.) by defining thelocale
value and translation object.
Locale keywords | Text |
---|---|
today | Name of the button to choose Today date. |
placeholder | Hint to describe expected value in input element. |
- Before changing to a culture other than
English
, ensure that locale text for the concerned culture is loaded throughload
method ofL10n
class.
//Load the L10n, loadCldr from ej2-base
import { loadCldr, L10n } from "@syncfusion/ej2-base";
//load the locale object to set the localized placeholder value
L10n.load({
de: {
datetimepicker: {
placeholder: "Wählen Sie ein Datum und eine Uhrzeit aus",
today:"heute"
}
}
});
- Set the culture by using the
locale
property.
In the following code example, the DateTimePicker is initialized inGerman
culture with corresponding localized text.
The following example demonstrates the DateTimePicker in German
culture.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars'
import { Component } from "@angular/core";
import { DateTimePickerComponent } from "@syncfusion/ej2-angular-calendars";
import { loadCldr, L10n } from "@syncfusion/ej2-base";
// Here we have referred local json files for preview purpose
import * as numberingSystems from "./numberingSystems.json";
import * as gregorian from "./ca-gregorian.json";
import * as numbers from "./numbers.json";
import * as detimeZoneNames from "./timeZoneNames.json";
loadCldr(numberingSystems, gregorian, numbers, detimeZoneNames);
@Component({
imports: [
DateTimePickerModule
],
standalone: true,
selector: 'app-root',
template: `<ejs-datetimepicker [value]='date' locale='de'></ejs-datetimepicker>`
})
export class AppComponent {
public date: Date = new Date("12/11/2017 1:00 AM");
ngOnInit(): void {
/*loads the localization text*/
L10n.load({
de: {
datetimepicker: {
placeholder: "Wählen Sie ein Datum und eine Uhrzeit aus",
today:"heute"
}
}
});
}
constructor() {}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Right-To-Left
The DateTimePicker supports RTL (right-to-left) functionality for languages like Arabic and Hebrew to displays the text in the right-to-left direction.
UseenableRtl
property to set the RTL direction.
The following code example initialize the DateTimePicker component in Arabic
culture and also explains how to set the localized text to the placeholder using load
method of L10n
class.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars'
import { Component } from '@angular/core';
import { DateTimePickerComponent } from '@syncfusion/ej2-angular-calendars';
import { loadCldr, L10n } from '@syncfusion/ej2-base';
// Here we have referred local json files for preview purpose
import * as numberingSystems from './numberingSystems.json';
import * as gregorian from './ca-gregorian.json';
import * as numbers from './numbers.json';
import * as artimeZoneNames from './timeZoneNames.json';
loadCldr(numberingSystems, gregorian, numbers, artimeZoneNames);
@Component({
imports: [
DateTimePickerModule
],
standalone: true,
selector: 'app-root',
template: `<ejs-datetimepicker [enableRtl]='true' locale='ar'></ejs-datetimepicker>`
})
export class AppComponent {
ngOnInit(): void {
L10n.load({
'ar': {
'datetimepicker': {
placeholder: 'حدد التاريخ والوقت',
today: "اليوم"
}
}
});
}
constructor() {}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));