Telerik UI for ASP.NET MVC Globalization Overview
Globalization is the process of designing and developing an application that works in multiple cultures and languages.
It combines localization (the translation of component messages) with internationalization (their adaptation to a specific culture). Cultures require and define particular information for their number formats, week and month names, date and time formats, and so on.
All Kendo UI widgets and their ASP.NET MVC server-side wrappers which support date or number formatting depend also on the current culture. Usually, such components are more complex (for example, the Grid, ListView, Charts, and so on).
The following Telerik UI for ASP.NET MVC helpers depend on the current culture:
- Calendar
- DateInput
- DatePicker
- TimePicker
- DateTimePicker
- NumericTextBox
- MaskedTextBox (globalized mask literals)
- Scheduler
- Gantt
- Grid
- Chart
- ListView
Applying Cultures
To use a culture that is different from the default en-US
one in Telerik UI for ASP.NET MVC:
Copy the required culture JavaScript file from the
\js\culture\
folder of your Telerik UI for ASP.NET MVC installation to the~/Scripts/cultures/
folder of your application. Alternatively, provide the culture files by using the Kendo CDN service.-
Include the corresponding culture JavaScript file after the other JavaScript product files. This example uses the Spanish
es-ES
culture.<script src="@Url.Content("~/Scripts/jquery.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo.all.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script> <script src="@Url.Content("~/Scripts/cultures/kendo.culture.es-ES.min.js")"></script>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script> <script src="https://kendo.cdn.telerik.com/<version>/js/kendo.all.min.js"></script> <script src="https://kendo.cdn.telerik.com/<version>/js/kendo.aspnetmvc.min.js"></script> <script src="https://kendo.cdn.telerik.com/<version>/js/cultures/kendo.culture.es-ES.min.js"></script>
-
Set the current culture by calling the
kendo.culture
method. You have to add the script block after the culture JavaScript file. As a result, all Telerik UI for ASP.NET MVC helpers will use thees-ES
culture for parsing and formatting dates and numbers.<script> kendo.culture("es-ES"); </script>
You can find the complete list of available cultures in the Kendo UI Core repository.
Matching Cultures
The cultures that are set on the client and on the server have to match. This ensures that dates and numbers are displayed and parsed correctly.
Setting the Server-Side Culture
You can set the server-side culture either globally or per request.
To set the server-side culture globally, update the web.config
file of your ASP.NET MVC application.
<system.web>
<!-- snip --!>
<globalization uiCulture="es-ES" culture="es-ES"></globalization>
<!-- snip --!>
</system.web>
To set the server-side culture per request, override the Controller.Initialize
method to set the CurrentCulture
and CurrentUICulture
.
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(requestContext.HttpContext.Request["my-culture"]);
base.Initialize(requestContext);
}
Setting Matching Client-Side Cultures
To make the helpers use the same culture as the culture set on the server side:
- Copy the required culture JavaScript files from the
\js\culture\
folder of your Telerik UI for ASP.NET MVC installation to the~/Scripts/cultures/
folder of your application. -
Get the current culture.
@{ var culture = System.Globalization.CultureInfo.CurrentCulture.ToString(); }
-
Include the corresponding culture JavaScript file.
<script src="@Url.Content("~/Scripts/cultures/kendo.culture." + culture + ".min.js")"></script>
-
Set the current culture by calling the
kendo.culture
method. You have to add the script block after the culture JavaScript file.Set the client-side culture before initializing any helpers that rely on it.
<script> kendo.culture("@culture"); </script>
Using the Culture Helper
The Kendo UI culture scripts are generated based on the Windows 8 formats. If you use a different version that has different date or number formats, then data binding issues may occur. To avoid these side effects, use the Html.Kendo().Culture()
helper which generates the culture script based on the current .NET or specified culture.
The following example demonstrates how to generate the current and specified cultures.
@Html.Kendo().Culture()
@Html.Kendo().Culture("bg-BG")
The culture helper also provides the option to disable the rendering inside a script tag so that it can be included in the existing script.
The following example demonstrates how to generate the current and specified cultures in an existing script file.
<script>
@Html.Kendo().Culture(false)
</script>
<script>
@Html.Kendo().Culture("bg-BG", false)
</script>