Globalization in ASP.NET CORE Calendar

27 Aug 202611 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, Calendar date format, week and month names are specific to American English culture. The Calendar utilizes the Essential JavaScript 2 Internationalization package to parse and format the date object based on the culture. It uses the official UNICODE CLDR JSON data.

To use a culture other than English, follow the below steps.

  • Set the culture by using the locale property.

  • Install the cldr-data package by using the below command (it 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, you can find the culture specific JSON data under the location /node_modules/cldr-data/.

In ASP.NET Core, the static file contents should be present under wwwroot folder. For this, manually copy the cldr-data from the node_modules folder and place it inside the wwwroot folder and refer from the wwwroot/scripts/cldr-data location like the following code example.

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(location.origin + location.pathname + '/../../scripts/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);
        }
    }

The Localization library allows you to localize default text content of the Calendar. The Calendar component has static text for the 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 to a culture other than English, ensure that locale text for the desired culture is loaded through the load method of the L10n class.
      var L10n = ej.base.L10n;
       L10n.load({
            "de": {
                "calendar": {
                    "today": "heute"
                }
            }
        });

The following example demonstrates the Calendar in German culture.

<ejs-calendar id="calendar"></ejs-calendar>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        calendarObject = document.getElementById('calendar').ej2_instances[0];
        var L10n = ej.base.L10n;

        L10n.load({
            "de": {
                "calendar": {
                    "today": "heute"
                }
            }

        });
        loadCultureFiles('de');
        calendarObject.locale = 'de';
    });
    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(location.origin + location.pathname + '/../../scripts/cldr-data/supplemental/' + files[prop], 'GET', false);
            } else {
                ajax = new ej.base.Ajax(location.origin + location.pathname + '/../../scripts/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);
        }
    }
</script>

Right-to-left

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

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

<ejs-calendar id="calendar" enablertl=true></ejs-calendar>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        calendarObject = document.getElementById('calendar').ej2_instances[0];
        var L10n = ej.base.L10n;  
        L10n.load({
            "en": {
                "calendar": {
                    "today": "Today"
                }
            }
        });
        loadCultureFiles('ar');
        calendarObject.locale = 'ar';
    });

    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(location.origin + location.pathname + '/../../scripts/cldr-data/supplemental/' + files[prop], 'GET', false);
            } else {
                ajax = new ej.base.Ajax(location.origin + location.pathname + '/../../scripts/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);
        }
    }
</script>