Skip to content

DevExpress-Examples/asp-net-mvc-change-culture-on-the-fly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET MVC - How to change the current culture at runtime

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.

Implementation Details

1. Add and set up a ComboBox extension.

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();
}

2. Change the application culture at runtime.

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;  
    }  
}  

3. Build a multi-language UI in the application.

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.

3.1 Provide localized strings.

Create custom Resource Files for different cultures and populate them with static resource strings. In this example, we created the following resources files:

The resource strings maintained in the files can be accessed at runtime as properties of the LocalizationText class.

3.2 Display localized strings in UI elements.

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; }

Files to Review

Documentation