Multi-lingual web application - how do I detect the user's language in ASP.NET?

后端 未结 6 807
说谎
说谎 2020-12-15 05:20

I\'m building an ASP.NET web application, and all of my strings are stored in a resource file. I\'d like to add a second language to my application, and ideally, I\'d like t

6条回答
  •  眼角桃花
    2020-12-15 05:53

        /// 
        /// Sets a user's Locale based on the browser's Locale setting. If no setting
        /// is provided the default Locale is used.
        /// 
    
    public static void SetUserLocale(string CurrencySymbol, bool SetUiCulture)
    {
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.UserLanguages == null)
            return;
    
        string Lang = Request.UserLanguages[0];
        if (Lang != null)
        {
            // *** Problems with Turkish Locale and upper/lower case
            // *** DataRow/DataTable indexes
            if (Lang.StartsWith("tr"))
                return;
    
            if (Lang.Length < 3)
                Lang = Lang + "-" + Lang.ToUpper();
            try
            {
                System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo(Lang);
                if (CurrencySymbol != null && CurrencySymbol != "")
                    Culture.NumberFormat.CurrencySymbol = CurrencySymbol;
    
                System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
    
                if (SetUiCulture)
                    System.Threading.Thread.CurrentThread.CurrentUICulture = Culture;
            }
            catch
            { ;}
        }
    }
    

    The source of this article is here: How to detect browser language

提交回复
热议问题