2

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?

Nacimota
  • 22,395
  • 6
  • 42
  • 44
  • Check out this question : https://stackoverflow.com/questions/7000509/how-to-change-currentculture-at-runtime – Srikanth May 29 '17 at 03:40

4 Answers4

0

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.

  • in page_load, but the problem is that this method InitializeCulture() runs before the page_load, so the session is always empty. – Mohammad AlShaabi May 29 '17 at 11:12
  • Try..Catch block doesn't really do anything because it seems like the exception is actually coming from Visual Studio itself. Here are the odd behaviors: - If I just run the code it works fine – Joshua Mason Apr 08 '19 at 06:17
0

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;
}
Kelly
  • 6,992
  • 12
  • 59
  • 76
0

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>
Srikanth
  • 86
  • 1
  • 4
  • 11
  • I tried this several times and it never changes the culture to the selected one, it always gives me the OS current culture no matter what. thanks for your reply. – Mohammad AlShaabi May 29 '17 at 11:10
  • Have a look at this https://www.codeproject.com/Articles/18753/Developing-an-ASP-NET-page-with-MasterPage-and-Loc – Srikanth May 30 '17 at 03:27
0

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.