I am trying to create a page with multi-language option in VB.net. The files are as follows:
Page (Folder)
- Sample.aspx
- Sample.aspx.vb
- App_LocalResources
- - Sample.aspx.nl.resx
- - Sample.aspx.nl.resx
The culture in web.config is as follows:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="nl" culture="nl" enableClientBasedCulture="true"/>
Then, on the Sample.aspx.vb
, I am trying to change the culture to "EN" but it still shows the "NL" text.
Protected Overrides Sub OnInit(e As System.EventArgs)
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en")
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en")
End Sub
I also tried putting it in Page_Load but it did not work. I tried to put a break on the code behind, it seems the the culture is changing but still shows the "NL" text. I already searched in www, but I did not find any solutions.
I am hoping the you can help me with this. What is the best solution? Should I just set the labels from code behind?
CodePudding user response:
The way ASP.NET WebForms treats culture is explained in How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization.
First of all, the culture can be set either in web.config
or at the page level with :
<globalization uiCulture="es" culture="es-MX" />
or
<%@ Page UICulture="es" Culture="es-MX" %>
It's also possible to set the culture based on the Accept-Language
header sent by the browser by setting the culture to auto
:
<globalization uiCulture="auto" culture="auto" />
Finally, to modify the request's culture programmatically, override the InitializeCulture
method:
Protected Overrides Sub InitializeCulture()
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en")
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en")
End Sub
This is useful when the browser doesn't send an Accept-Language
header or when we want to apply our own logic, using eg geolocation to determine the language if Accept-Language
is missing