Globalization in EJ2 JavaScript Calendar control

16 Jun 202315 minutes to read

Globalization is the combination of adapting the component to various languages by means of parsing and formatting the date or number Internationalization and also by adding cultural specific customizations and translating the text localization

By default, the Calendar date format, week, and month names are specific to American English culture. It uses the Essential JavaScript 2 Internationalization package to parse and format date object based on the culture using the official UNICODE CLDR JSON data. It provides the loadCldr method to load the culture-specific CLDR JSON data.

All the Essential JS 2 component are specific to English culture (‘en-US’). If you want to go with the different culture other than English, follow the below steps.

  • Install the CLDR-Data package by using the below command (installs the CLDR JSON data). To know more about CLDR data, refer to the CLDR-Data link.
npm install cldr-data --save

Once the package is installed, the culture-specific JSON data will be available in /node_modules/cldr-data.

  • Now, import the installed CLDR JSON data to the app.ts file. To import JSON data, install the JSON plugin loader. In the example, SystemJS JSON plugin loader is used.
npm install systemjs-plugin-json --save-dev
  • After it is installed, configure the system.config.js settings as given below to map the systemjs-plugin-json loader.
System.config({
    paths: {
        'npm:': './node_modules/',
        'syncfusion:': 'npm:@syncfusion/'

    },
    map: {
        app: 'app',

        //Syncfusion packages mapping
        "@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js",
        "@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js",
        "@syncfusion/ej2-inputs": "syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js",
        "@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js",
        "@syncfusion/ej2-splitbuttons": "syncfusion:ej2-splitbuttons/dist/ej2-splitbuttons.umd.min.js",
        "@syncfusion/ej2-lists": "syncfusion:ej2-lists/dist/ej2-lists.umd.min.js",
        "@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js",
        "@syncfusion/ej2-calendars": "syncfusion:ej2-calendars/dist/ej2-calendars.umd.min.js",
        "cldr-data": 'npm:cldr-data',
        //systemjs-plugin-json loader package mapping
        "plugin-json": "npm:systemjs-plugin-json/json.js"
    },
    meta: {
        '*.json': { loader: 'plugin-json' }
    },
    packages: {
        'app': { main: 'app', defaultExtension: 'js' },
        'cldr-data': { main: 'index.js', defaultExtension: 'js' }
    }
});

System.import('app');
  • Now, use the loadCldr method to load the culture-specific CLDR JSON data from the installed location to app.ts file.

  • Calendar displayed Sunday as the first day of week based on default culture (“en-US”). If you want to display the Calendar with loaded culture’s first day of week, you need to import weekdata.json file from the cldr-data/suppemental as 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

The Localization library allows you to localize default text content of the Calendar. The Calendar component has static text for today feature that can be changed to other cultures (Arabic, Deutsch, French, etc.) by defining the locale value and translation object.

Locale keywords Text
today Name of the button to choose Today date.
  • Before changing the culture other than English, ensure that locale text for the concerned culture is loaded through load method of
    L10n class.
//Load the L10n from ej2-base
import { L10n } from '@syncfusion/ej2-base';

//load the locale object to set the localized placeholder value
L10n.load({
    'de': {
        'calendar': { today: 'heute'}
    }
});
  • Set the culture by using the locale property. The code example initializes the Calendar component in the German culture.
import { Calendar } from '@syncfusion/ej2-calendars';
//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'));

L10n.load({
    'de': {
        'calendar': { today: 'heute' }
    }
});

let calendarObject: Calendar = new Calendar({
    //sets the locale.
    locale: 'de'
});
calendarObject.appendTo('#element');

The following example displays the Calendar in German culture.

ej.base.enableRipple(true);
var L10n = ej.base.L10n;
   L10n.load({
    'de': {
        'calendar': { today:'heute'}
    }
  });
     loadCultureFiles('de');
      var calendar = new ej.calendars.Calendar({
       locale: 'de'
    });
    calendar.appendTo('#element');

  function loadCultureFiles(name) {
        var files = ['ca-gregorian.json', 'numbers.json', 'timeZoneNames.json'];
        var loader = ej.base.loadCldr;
        var loadCulture = function (prop) {
            var val, ajax;
            ajax = new ej.base.Ajax('https://ej2.syncfusion.com/javascript/demos/' + 'src/common/cldr-data/main/' + name + '/' + files[prop], 'GET', false);
            ajax.onSuccess = function (value) {
                val = value;
            };
            ajax.send();
            loader(JSON.parse(val));
        };
        for (var prop = 0; prop < files.length; prop++) {
            loadCulture(prop);
        }
    }
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Calendar control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <!--style reference from the Calendar component-->
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
    <!--style reference from app-->
    <link href="styles.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <!--element which is going to render the Calendar-->
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Right-to-left

The Calendar supports right-to-left functionality for languages like Arabic, Hebrew, etc. to display text in the right-to-left direction. Use
enableRtl property to set the RTL direction.

The following code example initializes the Calendar component in Arabic culture.

import { Calendar } from '@syncfusion/ej2-calendars';
//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/ar/ca-gregorian.json'),
    require('cldr-data/main/ar/numbers.json'));
    require('cldr-data/main/ar/timeZoneNames.json'));

L10n.load({
    'ar': {
        'calendar': {
            today: 'اليوم'
        }
    }

});

let calendarObject: Calendar = new Calendar({
    //sets the locale.
    locale: 'ar'
});
calendarObject.appendTo('#element');

The following example displays the Calendar in Arabic
culture in the right-to-left direction.

ej.base.enableRipple(true);
var L10n = ej.base.L10n;
L10n.load({
    'ar': {
        'calendar': { today: 'اليوم'}
    }
  });

loadCultureFiles('ar');
   var calendar = new ej.calendars.Calendar({
       locale: 'ar',enableRtl:true
    });
    calendar.appendTo('#element');
 
function loadCultureFiles(name) {
        var files = ['ca-gregorian.json', 'numbers.json', 'timeZoneNames.json'];
        if (name === 'ar') {
            files.push('numberingSystems.json');
        }
        var loader = ej.base.loadCldr;
        var loadCulture = function (prop) {
            var val, ajax;
            if (name === 'ar' && prop === files.length - 1) {
                ajax = new ej.base.Ajax('https://ej2.syncfusion.com/javascript/demos/src/common/cldr-data/supplemental/' + files[prop], 'GET', false);
            } else {
                ajax = new ej.base.Ajax('https://ej2.syncfusion.com/javascript/demos/src/common/cldr-data/main/' + name + '/' + files[prop], 'GET', false);
            }
            ajax.onSuccess = function (value) {
                val = value;
            };
            ajax.send();
            loader(JSON.parse(val));
        };
        for (var prop = 0; prop < files.length; prop++) {
            loadCulture(prop);
        }
    }
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Calendar control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <!--style reference from the Calendar component-->
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="styles.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <!--element which is going to render the Calendar-->
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>