views:

54

answers:

2

Hi. How can i change system culture that affects all pages(programmatically)? i saw this thread.

but i want to change culture in my Login page with this codes.

protected void btnChangeLanguage(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
}

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa-IR")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fa-IR")

My Culture Changed by top Code. I checked by this code.

MsgBox("Current Culture is " + CultureInfo.CurrentCulture.EnglishName)

but still our text and labels are english. (i have english resource and persian resource.)

+3  A: 

For a list of all cultures, see the documentation for the CultureInfo class

Regarding your other question, you can save a cookie with the language code as its value, and read it in the overriden InitializeCulture as follows:

Set the cookie with the language code as its value as soon as the user clicks the button:

protected void btnChangeLanguage_Click(object sender, EventArgs e)
{
    Response.Cookies.Add(new HttpCookie("language") { 
        Value="es",
        Expires=DateTime.Now.AddDays(30) /* A sample timestamp */
    });
}

(Little side note: I've appended a _Click to the handler's name, since it may cause a conflict with the button's name in the definition)

Read the cookie as soon as the InitializeCulture method is invoked during the early stages of the page's lifecycle:

protected override void InitializeCulture()
{
    var cookie = Request.Cookies["language"];
    if (cookie != null)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Value);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
    }
    base.InitializeCulture();
}
Giu
Thank you how about other question?
shaahin
@shaahin You're welcome. I've updated my answer with a sample solution to your first question regarding the setting of the language
Giu
Thanks a lot. please put your code in Block :).
shaahin
Dear abatishchev. Thank You. :)
shaahin
@shaahin You're welcome! The first version of the answer displayed the code correctly, but it somehow was discarded after I changed some text. Strange thing :) @abatishchev Thanks for correcting it.
Giu
+2  A: 

Add following line to <system.web> section of your web.config

<globalization culture="es" uiCulture="es"/>
Marko
Thanks.How can i do it programmatically?
shaahin
You should override method InitCulture of your Page based class.
Marko
Actually, it is InitializeCulture() method. You can save culture code after login in a session variable and than use that value during InitializeCulture(). In that way different users can have different culture.
Marko