Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CurrentThread.CurrentUICulture is set correctly but get always en-US

I am working on a multilingual website with two languages to start with Arabic and English

I have set language to Arabic (ar-ae) in IE and also on FireFox but it always get me the English version i am not able to troubleshoot this issue

Site Structure

Default.aspx
 en/Default.aspx
 ar/Default.aspx

Problem is that when i use URL as http://localhost:49831/site/Defualt.aspx Then it works fine and redirects me to Arabic version ar/Default.aspx if Arabic is selected as language.

But when is use URL as http://localhost:49831/site/ then it always redirect me to English version en/Default.aspx

I am not sure what i am doing wrong.

I use Default.aspx only to detect the default browser language and then redirect accordingly

CODE for Default.aspx.cs file

    // Localization and Globalization code
    protected override void InitializeCulture()
    {
        String lang = Request["Language"];

        Session["lang"] = Helper.DetectLanguage(lang);
        //Set Direction of page LTR/RTL
        if (Session["lang"] == "ar-AE")
        {
            Session["PageDIR"] = "rtl";
        }
        else
        {
            Session["PageDIR"] = "ltr";
        }

        base.InitializeCulture();
    }

   public static String DetectLanguage(String lang)
    {
        String LangCode = lang;

        if (!string.IsNullOrEmpty(lang))
        {
            lang =  lang.ToLower();
        }

        String Lang2Char;
       CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
        String LangName = ci.Name.ToString().ToLower();
        //check leng & if length is less than 2 then set english as default language.
        if (LangName.Length > 1)
        {
             Lang2Char = LangName.Substring(0, 2);
        }
        else
        {
             Lang2Char = "en";
        }

        // if QueryString is not null then execute foollowinf if block
        //If Language is present in Querystring then excute if part else, else part
        if (lang != null)
        {
            switch (lang)
            {
                case "en-us":
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                    LangCode = "en-US";
                    break;
                case "ar-ae":
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-AE");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-AE");
                    LangCode = "ar-AE";
                    break;
                default:
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                    LangCode = "en-US";
                    break;
            }
        }
        // if lang query string is null then set the language based on following logic
        else
        {
            switch (Lang2Char)
            {
                case "en":
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        LangCode = "en-US";
                        break;
                case "ar":
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-AE");
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-AE");
                        LangCode = "ar-AE";
                        break;
                default:
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                        LangCode = "en-US";
                        break;
            }

        }
        return LangCode;
    }

How can i best define this code so that it works. Is it better to define CurrentUICulture in Global.asx file

like image 423
Learning Avatar asked Dec 17 '12 13:12

Learning


2 Answers

I would advise you to take a little bit different approach when dealing with multiple language site. For start you don't have to use multiple Default pages, you need just one.

So let me explain how this can be done.

First, default site globalization can be set inside web.config:

  <system.web>
    <globalization uiCulture="sl-SI" culture="sl-SI" enableClientBasedCulture="false"/>
  </system.web>

You can set enableClientBasedCulture="true" to force client site globalization if possible. If not, default culture will take place.

Next, how you can set globalization inside asp.net page (force, when user requires):

    internal static void ChangeLanguage(string languageCode, System.Web.UI.Page page)
    {
        CultureInfo ci = new CultureInfo(languageCode);
        page.Session[cLanguageSessionKey] = ci;
        //save to user profile is required (just a sample)...
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            Entities.MembershipUserFull user = Services.MembershipUserManager.GetMembershipUserFull(HttpContext.Current.User.Identity.Name);
            user.LanguageId = Services.LanguageManager.GetLanguageIdFromLanguageCode(languageCode);

            //save profile... 
            user.SaveProfile();
        }

        //reload page. New culture will take efect...
        page.Response.Redirect(page.Request.Url.AbsoluteUri);
    }

Here is for Page OnLoad:

        //set language if required...
        if (User.Identity.IsAuthenticated)
        {
            Entities.MembershipUserFull user = Services.MembershipUserManager.GetMembershipUserFull(HttpContext.Current.User.Identity.Name);
            if (user != null && user.LanguageId != null)
            {
                GP.Solutions.Entities.Language language = Services.LanguageManager.GetLanguage((int)user.LanguageId);
                if (Thread.CurrentThread.CurrentUICulture.Name != language.Code)
                {
                    ChangeLanguage(language.Code, this);
                }
            }
        }

And finally how multilanguage support is handled on web controls:

Take notice I have two global resources (one for each supported language. The one without language code is default)

enter image description here

Let's say I need to display some text on Label:

<asp:Label ID="Label1" runat="server" CssClass="AdvertPreTitle" Text='<% $resources:AppResource,MostPopularService %>'></asp:Label>

Take notice MostPopularService is read from specific language resource according to current UI Culture. Here is snipp from resource file:

enter image description here

So in the end I have only one Default page that handles all the languages.

like image 164
Gregor Primar Avatar answered Oct 21 '22 05:10

Gregor Primar


According to this MSDN link, enableClientBasedCulture="true" does not work anymore. Instead, do the following:

  <system.web>    
     <globalization culture="auto" uiCulture="auto"/>
  </system.web>
like image 37
Shreyas Avatar answered Oct 21 '22 06:10

Shreyas