Globalization in Angular Calendar
31 Aug 20268 minutes to read
Globalization is the combination of internationalization and localization. You can adapt the component to various languages by parsing and formatting the date or number using the Internationalization library, and also add culture-specific customization and translation to the text using the Localization library.
By default, the Calendar date format, week, and month names are specific to the American English culture. It uses 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, and also provides the loadCldr method to load the culture-specific CLDR JSON data.
To use a culture other than English, follow the below steps. Replace de in the code with your culture code.
-
Install the
CLDR-Datapackage by using the below command (it installs the CLDR JSON data). To know more about CLDR-Data, refer theCLDR-Datalink.npm install cldr-data --saveOnce the package is installed, you can find the culture-specific JSON data under the location
/node_modules/cldr-data. -
Import the installed CLDR JSON data into the
app.component.tsfile, and use theloadCldrmethod to load the culture-specific CLDR JSON data from the installed location into theapp.component.tsfile.By default, the Calendar displays
Sundayas the first day of the week based on the default culture (“en-US”). If you want to display the Calendar with the loaded culture’s first day of week, import theweekdata.jsonfile fromcldr-data/supplementalas given in the code example.// import the loadCldr from ej2-base import { loadCldr } from '@syncfusion/ej2-base'; declare var require: any; loadCldr( require('cldr-data/supplemental/numberingSystems.json'), require('cldr-data/main/de/ca-gregorian.json'), require('cldr-data/main/de/numbers.json'), require('cldr-data/main/de/timeZoneNames.json'), require('cldr-data/supplemental/weekdata.json')); // To load the culture-based first day of week
Localization
The
Localizationlibrary allows you to localize default text content of the Calendar. The Calendar component has static text for thetodayfeature that can be changed to other cultures (Arabic, Deutsch, French, etc.) by defining thelocalevalue and translation object.
The following table lists the locale keys (L10n keys) supported by the Calendar component. For the Calendar, today is the only localizable string.
| Locale key | Text |
|---|---|
today |
Name of the button to choose the Today date. |
Before changing to a culture other than English, ensure that the locale text for the concerned culture is loaded through the load method of the L10n class.
//Load the L10n, loadCldr from ej2-base
import { loadCldr, L10n } from "@syncfusion/ej2-base";
//load the locale object to set the localized today value
L10n.load({
de: {
calendar: {
today: "heute"
}
}
});Set the culture by using the locale property. The below code example initializes the Calendar component in German culture. Note that weekdata.json is required only when you want to render the culture-specific first day of week; it is omitted here for brevity.
import { Component } from '@angular/core';
// import the loadCldr from ej2-base
import { loadCldr, L10n } from '@syncfusion/ej2-base';
declare var require: any;
loadCldr(
require('cldr-data/supplemental/numberingSystems.json'),
require('cldr-data/main/de/ca-gregorian.json'),
require('cldr-data/main/de/numbers.json'),
require('cldr-data/main/de/timeZoneNames.json')
);
@Component({
selector: 'app-root',
template: `
<!-- Sets the value, locale -->
<ejs-calendar [value]='dateValue' locale='de'></ejs-calendar>`
})
export class AppComponent {
public dateValue: Object = new Date();
ngOnInit(): void {
/*loads the localization text*/
L10n.load({
'de': {
'calendar': {
today: "heute"
}
}
});
}
constructor() {
}
}The following example demonstrates the Calendar in German culture.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { CalendarModule } from '@syncfusion/ej2-angular-calendars'
import { Component } from '@angular/core';
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 timeZoneNames from './timeZoneNames.json';
loadCldr(numberingSystems, gregorian, numbers, timeZoneNames);
@Component({
imports: [
CalendarModule //declaration of ej2-angular-calendars module into NgModule
],
standalone: true,
selector: 'app-root',
template: `
<!-- Sets the value, locale -->
<ejs-calendar [value]='dateValue' locale='de'></ejs-calendar>`
})
export class AppComponent {
public dateValue: Object = new Date();
ngOnInit(): void {
/*loads the localization text*/
L10n.load({
'de': {
'calendar': {
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 Calendar supports right-to-left functionality for languages like Arabic and Hebrew to display the text in the right-to-left direction. Use the enableRtl property to set the RTL direction. For RTL cultures such as Arabic, you must also load the appropriate main CLDR files (e.g., cldr-data/main/ar/*) using loadCldr in addition to setting enableRtl.
The following example demonstrates the Calendar in Arabic culture with the enableRtl property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { CalendarModule } from '@syncfusion/ej2-angular-calendars'
import { Component } from '@angular/core';
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 timeZoneNames from './timeZoneNames.json';
loadCldr(numberingSystems, gregorian, numbers, timeZoneNames);
@Component({
imports: [
CalendarModule //declaration of ej2-angular-calendars module into NgModule
],
standalone: true,
selector: 'app-root',
template: `
<!-- Sets the value, locale,enableRtl -->
<ejs-calendar [value]='dateValue' locale='ar' enableRtl='true'></ejs-calendar>
`
})
export class AppComponent {
public dateValue: Object = new Date();
ngOnInit(): void {
L10n.load({
'ar': {
'calendar': { today: "اليوم"}
}
});
}
constructor() {
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));