how to pass the selected culture into InitializeCulture() method

1.7k views Asked by At

I have a login page where the user can select his language through a radio button, after login the user is redirected to Default.aspx where I'm using the below method to set the page culture:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<%@ Import Namespace="System.Resources" %>
<% @Import Namespace="System.Globalization" %>
<% @Import Namespace="System.Threading" %>
<script runat=server>
        protected override void InitializeCulture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["lang"].ToString());
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["lang"].ToString());
            base.InitializeCulture();
        }
</script>

How can I pass the selected language from the login page to this method in Default.aspx page? I tried to pass it through session but I got this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Web.SessionState.HttpSessionState.this[string].get returned null.

Because this method happens in early stages before any control or session is initiated, that's why it returns null. Any idea how can I pass the selected culture to this method?

4

There are 4 answers

0
Mohammad AlShaabi On BEST ANSWER

I found the answer, I managed to pass the selected culture info using Query String:

in my login page I have those two radio buttons and a button:

<asp:RadioButton ID="RadioButton1" runat="server" Text="ar-AE" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="en-US" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

in the code behind the button OnClick:

protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            Response.Redirect("Default.aspx?language=" + RadioButton1.Text);
        }
        else
        {
            Response.Redirect("Default.aspx?language=" + RadioButton2.Text);
        }
    }

then in Default.aspx page I have this code:

<% @Import Namespace="System.Resources" %>
<% @Import Namespace="System.Globalization" %>
<% @Import Namespace="System.Threading" %>
<script runat=server>
    protected override void InitializeCulture()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.QueryString["language"].ToString());
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.QueryString["language"].ToString());
        base.InitializeCulture();
    }
</script>

I tried to use Cookie also but it returns null as well like the session, so the query string is the only way that worked for me.

2
Joshua Mason On

Where in your code is "lang" loaded into Session?

I believe this is telling you that "lang" doesn't exist in SessionState vs. that SessionState itself is not available.

1
Kelly On

Are you setting it as a default or allowing the user to choose it?

If the former then just put this in your App.xaml.cs:

[assembly: NeutralResourcesLanguage("en-US")]

Could also do something like this if you worried about thread culture:

var culture = CultureInfo.CreateSpecificCulture("en-US");

if (Thread.CurrentThread.CurrentCulture.Name != "en-US")
{
    var culture = CultureInfo.CreateSpecificCulture("en-US");
    CultureInfo.DefaultThreadCurrentCulture = culture;
    CultureInfo.DefaultThreadCurrentUICulture = culture;
}
2
Srikanth On

Change Culture of a webpage as in MSDN

<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected override void InitializeCulture()
{
    if (Request.Form["ListBox1"] != null)
    {
        String selectedLanguage = Request.Form["ListBox1"];
        UICulture = selectedLanguage ;
        Culture = selectedLanguage ;

        Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}
</script>