Current filter:
                                You should refresh the page.
                                  • Description:
                                    This Knowledge Base article raises a question how to apply different cultures in your ASP.NET MVC Application on the "fly". Subjects discussed here are the following:

                                    1. A general way of changing culture settings in an ASP.NET MVC Application at runtime.
                                    2. Building a UI to change culture settings in the ASP.NET MVC Application.
                                    3. Building a multi-language UI in the ASP.NET MVC Application.

                                    Answer:
                                    1. A general way of changing culture settings in the ASP.NET MVC Application at runtime.

                                    To switch a culture in the ASP.NET MVC Application at runtime, you need to adjust the Thread.CurrentUICulture and Thread.CurrentCulture properties. One of the ways to adjust these properties across the entire application is handling the HttpApplication.AcquireRequestState Event  in the Global.asax file. For instance:

                                    [C#]
                                    protected void Application_AcquireRequestState(object sender, EventArgs e) { CultureInfo ci = new CultureInfo("de-DE"); Thread.CurrentThread.CurrentUICulture = ci; Thread.CurrentThread.CurrentCulture = ci; }
                                    [VB.NET]
                                    Protected Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs) Dim ci As New CultureInfo("de-DE") Thread.CurrentThread.CurrentUICulture = ci Thread.CurrentThread.CurrentCulture = ci End Sub

                                    2. Building a UI to change culture settings in the ASP.NET MVC Application.

                                    I will show how to use our ComboBox Extension to change a culture in your project via a drop-down editor. I am going to store combo box values between requests in cookies. Therefore, I will use the combo box client-side API to set and retrieve a cookies value. To send requests to the server when the combo box selected index is changed, I will wrap the combobox into the Html Form and submit the Form on the combo box value change. Finally, I will change the HttpApplication.AcquireRequestState Event handler code to get the cookies value and apply a specific culture. Refer to the code below.

                                    JavaScript:

                                    [JavaScript]
                                    function Init(s, e) { s.SetValue(ASPxClientUtils.GetCookie("Culture")); } function SelectedIndexChanged(s) { ASPxClientUtils.SetCookie("Culture", s.GetValue()); $("#form").submit(); }

                                    Shared Layout View:

                                    [C#]
                                    @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() }
                                    [VB.NET]
                                    @Using (Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With {.id = "form"})) Html.DevExpress().ComboBox( Sub(settings) settings.Name = "ComboBox" settings.Width = 180 settings.SelectedIndex = 0 settings.Properties.ValueType = GetType(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" End Sub).GetHtml() End Using

                                    Global.asax.cs:

                                    [C#]
                                    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; } }
                                    [VB.NET]
                                    Protected Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs) If Request.Cookies("Culture") IsNot Nothing AndAlso (Not String.IsNullOrEmpty(Request.Cookies("Culture").Value)) Then Dim culture As String = Request.Cookies("Culture").Value Dim ci As New CultureInfo(culture) Thread.CurrentThread.CurrentUICulture = ci Thread.CurrentThread.CurrentCulture = ci End If End Sub

                                    3. Building a multi-language UI in the ASP.NET MVC Application.

                                    The final step is to get benefit from the applied approach and build a multi-language UI. Two separate approaches are listed below.

                                    3.1. Localization using Satellite Resource Assemblies.

                                    It is possible to localize the UI of our ASP.NET MVC Extensions using Satellite Resource Assemblies. I won't describe this approach in detail here because we have a great article on this subject in our online documentation:  Localizing ASP.NET Controls via Satellite Resource Assemblies. The following articles might be also helpful:

                                    DevExpress Localization Service
                                    A421 - The collection of localized DevExpress assemblies

                                    3.2 Using a custom localization approach.

                                    If you are building a custom UI on top of our MVC Extensions, you can translate your application into different languages by generating custom Resource Files and using static resource strings obtained from them. I am going to create three resource files: LocalizationText.resx (the default one), LocalizationText.en-US.resx (the English language) and LocalizationText.de-DE.resx (the German language). In this case, I will be able to access localized strings at runtime as the LocalizationText class static properties.

                                    For instance, refer to the following code showing how to use a resource file string to change a page title:

                                    [C#]
                                    <h2>@LocalizationText.HomePageTitle</h2>
                                    [VB.NET]
                                    <h2>@LocalizationText.HomePageTitle</h2>

                                    You can also use the resource string along with our MVC Extensions. For instance, the following code shows how to add items to the Menu Extension and set items' text depending on the culture applied to a website:

                                    [C#]
                                    @Html.DevExpress().Menu( settings => { settings.Name = "Menu"; settings.Items.Add(item => { item.Text = LocalizationText.MenuItemOne; }); settings.Items.Add(item => { item.Text = LocalizationText.MenuItemTwo; }); }).GetHtml()
                                    [VB.NET]
                                    @Html.DevExpress().Menu( Sub(settings) settings.Name = "Menu" settings.Items.Add( Sub(item) item.Text = LocalizationText.MenuItemTwo End Sub) settings.Items.Add( Sub(item) item.Text = LocalizationText.MenuItemTwo End Sub) End Sub).GetHtml()

                                    The final approach I would like to highlight is adjusting the editor's validation message text depending on the current culture. Let's take our TextBox Extension, bind it to some field in the Model and enable the editor's validation mechanism. The last thing is enabling model validation using the data annotation RequiredAttribute. This approach will allow us to configure the RequiredAttribute to load the validation message from our custom Resource File string. Refer to the code below for details:

                                    [C#]
                                    @using (Html.BeginForm("Post", "Home", FormMethod.Post)) { @Html.DevExpress().TextBox( settings => { settings.Name = "Name"; settings.Width = 200; settings.ShowModelErrors = true; settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText; }).Bind(Model.Name).GetHtml() @Html.DevExpress().Button( settings => { settings.Name = "Button"; settings.UseSubmitBehavior = true; }).GetHtml() }
                                    [VB.NET]
                                    @Using (Html.BeginForm("Post", "Home", FormMethod.Post)) Html.DevExpress().TextBox( Sub(settings) settings.Name = "Name" settings.Width = 200 settings.ShowModelErrors = True settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText End Sub).Bind(Model.Name).GetHtml() Html.DevExpress().Button( Sub(settings) settings.Name = "Button" settings.UseSubmitBehavior = True End Sub).GetHtml() End Using

                                    I hope that you enjoyed reading this article. Please note that you can download the complete code for this Knowledge Base article in the T108173 - How to change the current Culture using ComboBox Extension code example.
                                    If you have any additional questions related to ASP.NET localization, feel free to post them to our Support Center. Our Support Team will be more than happy to follow-up with you.

                                0 Solutions

                                Creation Date Importance Sort by