Using Request.UserLanguages to automatically change page language on PreRender

Discussion in 'ASP.NET / ASP.NET Core' started by bribissell, Jul 21, 2011.

  1. Found related info at this thread, but not really what I need....

    I am making a page which pulls from the user's browser their preferred language, via the Request.UserLanguages....which returns a two letter code (ex. "en") or detailed code (ex. "en-GB").

    I basically get the string of user languages (they are in order of preference) and store them in a string array. Then I use a loop to check if the language code in the first position of the string array is any of the codes for a certain language (another string array hard coded in).

    Is there a better way to do this? I'm noticing increased load time and am worried additional languages will further slow the page load...

    Code:
    if (!IsPostBack)
    
        {   //Holds possible user languages preferences to check client machine against
            String[] compJapaneseLang = { "ja-jp","ja","jp","jpn","euc","shift-jis" };
        }
    
     //Get client machines langugage preferences                
            String[] userLang = Request.UserLanguages;
    
    //Loop through variation of preferences from possible user langugaes
    
            for (int i = 0; i < compJapaneseLang.Length; i++)
            { 
                    //IF JAPANESE
                if (userLang.GetValue(0).ToString().ToLowerInvariant().Equals(compJapaneseLang.GetValue(i).ToString().ToLowerInvariant()))
    
    
                    cc.JapeneseObject();
            }
     

Share This Page