This example demonstrates how to use the DevExpress ComboBox extension to change an application's culture on the fly. When a user selects an item, the extension sends a request to the server to change the culture. Between requests, the combo box value is stored in cookies.
Wrap the ComboBox extension in the HTML Form element to submit the form and send a request to the server when the selected item is changed.
Handle the extension's client events to save and restore the current culture (selected item) in cookies.
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" })) {
@Html.DevExpress().ComboBox(
settings => {
settings.Name = "ComboBox";
settings.Width = 180;
settings.SelectedIndex = 0;
settings.Properties.ValueType = typeof(string);
settings.Properties.Items.Add("German", "de-DE");
settings.Properties.Items.Add("English", "en-US");
settings.Properties.ClientSideEvents.SelectedIndexChanged = "SelectedIndexChanged";
settings.Properties.ClientSideEvents.Init = "Init";
}
).GetHtml()
}
function Init(s, e) {
s.SetValue(ASPxClientUtils.GetCookie("Culture"));
}
function SelectedIndexChanged(s) {
ASPxClientUtils.SetCookie("Culture", s.GetValue());
$("#form").submit();
}
Specify Thread.CurrentCulture and Thread.CurrentUICulture properties in the HttpApplication.AcquireRequestState to apply a culture in an ASP.NET MVC Application at runtime.
protected void Application_AcquireRequestState(object sender, EventArgs e) {
if (Request.Cookies["Culture"] != null && !string.IsNullOrEmpty(Request.Cookies["Culture"].Value)) {
string culture = Request.Cookies["Culture"].Value;
CultureInfo ci = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;
}
}
Use satellite resource assemblies to localize DevExpress MVC Extensions. This technique is described in detail in the following document and is not covered in this example: Satellite Resource Assemblies. This example demonstrates how to localize your custom UI elements on top of our MVC Extensions.
Create custom Resource Files for different cultures and populate them with static resource strings. In this example, we created the following resources files:
- LocalizationText.resx - the default resource file.
- LocalizationText.en-US.resx contains English resource strings.
- LocalizationText.de-DE.resx contains German resource strings.
The resource strings maintained in the files can be accessed at runtime as properties of the LocalizationText
class.
This section lists examples how to display the localized strings in the following UI elements:
-
A page title.
<h2>@LocalizationText.HomePageTitle</h2>
-
Custom items in DevExpress Menu extension:
@Html.DevExpress().Menu( settings => { settings.Name = "Menu"; settings.Items.Add(item => { item.Text = LocalizationText.MenuItemOne; }); settings.Items.Add(item => { item.Text = LocalizationText.MenuItemTwo; }); }).GetHtml()
-
An editor's validation message text.
Bind an editor to a field in the Model and enable the
ShowModelErrors
property to show a model error message in the editor.@Html.DevExpress().TextBox( settings => { settings.Name = "Name"; settings.ShowModelErrors = true; settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText; }).Bind(Model.Name).GetHtml()
Configure the
Required
field attribute to load the validation message from the custom resource file string.[Required(ErrorMessageResourceName = "RequiredValidationMessage", ErrorMessageResourceType = typeof(LocalizationText))] public string Name { get; set; }
- Global.asax.cs (VB: Global.asax.vb)
- Index.cshtml (VB: Index.vbhtml)
- _Layout.cshtml (VB: _Layout.vbhtml)
- Data.cs (VB: Data.vb)