aspnetcore-400577-devextreme-based-controls-concepts-localization.md
Localization set-up consists of the following parts:
Localize date, number, and currency values. There are two methods to do this:
Localize messages. To do this, use dictionaries that contain localized messages for different languages. The DevExtreme distribution includes predefined dictionaries (the dx.messages.[lang].js files in your project’s wwwroot/js/devextreme/localization directory). Use these files as is or as a base for a custom dictionary.
Set the locale.
This document contains step-by-step instructions for the Intl and Globalize methods.
To set up localization based on the Intl object, follow the steps below.
Open the file where DevExtreme scripts are referenced (for example, the _Layout.cshtml file) and reference dictionaries before the closing head tag:
<head>
...
@* reference dictionaries *@
<script src="~/js/devextreme/localization/dx.messages.de.js"></script>
<script src="~/js/devextreme/localization/dx.messages.es.js"></script>
</head>
You should set the locale to apply string translation and data formatting. To do this, add the script block to Razor files where you configure DevExtreme-based controls or to the project’s layout that applies to all pages in the application and call the DevExpress.localization.locale method. For example, the following code sets German as the default locale:
<script>
DevExpress.localization.locale("de");
</script>
You can also set the locale based on the client locale or dynamically (for example, your application contains a control that allows to switch a language, and the locale can depend on the selected language).
Important
The Globalize package is outdated and potentially unsafe. Use Globalize at your own risk. We recommend switching to Intl for a more secure solution.
Follow the steps below to set up localization based on the Globalize library.
Ensure the Globalize modules and CLDR scripts are linked in the layout file (usually named _Layout.cshtml). If you use a static asset bundler, register the resources in the bundler’s configuration files. Refer to the Bundle and minify static assets in ASP.NET Core article for more information.
<head>
//...
<script src="~/js/devextreme/cldr.js"></script>
<script src="~/js/devextreme/cldr/event.js"></script>
<script src="~/js/devextreme/cldr/supplemental.js"></script>
<script src="~/js/devextreme/cldr/unresolved.js"></script>
<script src="~/js/devextreme/globalize.js"></script>
<script src="~/js/devextreme/globalize/message.js"></script>
<script src="~/js/devextreme/globalize/number.js"></script>
<script src="~/js/devextreme/globalize/currency.js"></script>
<script src="~/js/devextreme/globalize/date.js"></script>
//...
</head>
These files are added and linked in the project in the following cases:
The corresponding code lines are commented out, you need to uncomment them.
The Globalize library requires CLDR data files. To get and add these files to the project:
Ensure Node.js is installed on your computer.
Create a temp directory anywhere on your computer.
Open the console from this directory and run the following commands:
Copy the cldr-data directory to the <your_project>/wwwroot directory.
Note
The cldr-data directory contains data for a large number of languages. To reduce the directory’s size, you can remove unnecessary languages data from the cldr-data/main subdirectory.
To load CLDR JSON files, open your controller’s code and create an action method that uses the CldrDataScriptBuilder helper.
Important
If you use Razor Pages, you should first create a controller.
The code below demonstrates a Home controller with a CldrData action. This action loads CLDR JSONs for German and Spanish locales from the node_modules/cldr-data directory and sets German as the default locale:
public class HomeController : Controller {
public IActionResult Index() {
return View();
}
public ActionResult CldrData() {
return new DevExtreme.AspNet.Mvc.CldrDataScriptBuilder()
.SetCldrPath("~/wwwroot/cldr-data")
.SetInitialLocale("de")
.UseLocales("de", "es")
.Build();
}
}
You can also set the locale based on the client locale or dynamically (for example, your application contains a control that allows to switch a language, and the locale can depend on the selected language).
Open the file where DevExtreme scripts are referenced. Typically, it is the _Layout.cshtml file.
Before the closing head tag:
Note
Demo: Using Globalize
Here are a few situations when you may need a custom dictionary:
To create and use a custom dictionary:
You can configure controls so that they automatically detect the client browser’s locale and apply the corresponding UI translation and data formatting. To do this, add the script code block provided below to Razor files where you configure DevExtreme-based controls or to a project’s layout that applies to all pages in the application.
For Intl, call the DevExpress.localization.locale method and specify navigator.language as a parameter.
For Globalize, call the Globalize.locale method and specify navigator.language as a parameter.
See Also