locked
Cannot set value to CultureInfo object RRS feed

  • Question

  • User-866306878 posted

    This is my first time to use CultureInfo object to handle multiple language in a web site. I created several resource files and add the following entry to web.config

    <globalization uiCulture="en" culture="en"/>

    The following statement set new value when user select other languages

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);

    I found that CultureInfo object keeps to get original culture "en" when it go to other pages. How to fix this problem?

    Tuesday, September 8, 2020 5:17 AM

All replies

  • User-474980206 posted

    websites are stateless. you need to set the culture on every request.

    Tuesday, September 8, 2020 3:40 PM
  • User1686398519 posted

    Hi tonylck,

    1. Once you add the globalization element to the web.config file, it will be applied to all pages.
    2. The current UI culture is a per-thread property. That is, each thread has its own current UI culture.The CultureInfo object provides information about a specific culture.
      • For example, if you set CurrentCulture and CurrentUICulture to ar in the current action, CurrentCulture and CurrentUICulture are ar in the current request. After you request other actions, CurrentCulture and CurrentUICulture are the default en.
    3. Here is a solution, you can refer to it.
      • You can set the active culture in CommonController.
      • You can make all controllers inherit from CommonController, so that any controller derived from CommonController will ensure that its actions are set to the active application culture.

    Model

        public class Languages
        {
            public string LanguageFullName { get; set; }
            public string LanguageCultureName { get; set; }
        }

    CommonController

        public class CommonController : Controller
        {
            protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
            {
                string lang = null;
                HttpCookie langCookie = Request.Cookies["culture"];
                if (langCookie != null)
                {
                    lang = langCookie.Value;
                }
                else
                {
                    var userLanguage = Request.UserLanguages;
                    var userLang = userLanguage != null ? userLanguage[0] : "";
                    if (userLang != "")
                    {
                        lang = userLang;
                    }
                    else
                    {
                        lang = LanguageMang.GetDefaultLanguage();
                    }
                }
                new LanguageMang().SetLanguage(lang);
                return base.BeginExecuteCore(callback, state);
            }
        }

    LanguageMang

        public class LanguageMang
        {
            public static List<Languages> AvailableLanguages = new List<Languages> {
                new Languages {
                    LanguageFullName = "Defalut", LanguageCultureName = "en"
                },
                new Languages {
                    LanguageFullName = "ar", LanguageCultureName = "ar"
                }
            };
            public static bool IsLanguageAvailable(string lang)
            {
                return AvailableLanguages.Where(a => a.LanguageCultureName.Equals(lang)).FirstOrDefault() != null ? true : false;
            }
            public static string GetDefaultLanguage()
            {
                return AvailableLanguages[0].LanguageCultureName;
            }
            public void SetLanguage(string lang)
            {
                try
                {
                    if (!IsLanguageAvailable(lang)) lang = GetDefaultLanguage();
                    var cultureInfo = new CultureInfo(lang);
                    Thread.CurrentThread.CurrentUICulture = cultureInfo;
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
                    HttpCookie langCookie = new HttpCookie("culture", lang);
                    langCookie.Expires = DateTime.Now.AddYears(1);
                    HttpContext.Current.Response.Cookies.Add(langCookie);
                }
                catch (Exception) { }
            }
        }

    LanguageController

        public class LanguageController : CommonController
        {
            public ActionResult Index(string lang)
            {
                new LanguageMang().SetLanguage(lang);
                CultureInfo culture1 = CultureInfo.CurrentCulture;
                CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
                return View();
            }
        }

    HomeController

        public class HomeController : CommonController
        {
            public ActionResult Index()
            {
                CultureInfo culture1 = CultureInfo.CurrentCulture;
                CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
                return View();
            }
            public ActionResult About()
            {
                CultureInfo culture1 = CultureInfo.CurrentCulture;
                CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
                return View();
            }
        }

    Index(LanguageController)

    @model LanguageResourceDemo.Models.Languages
    <ul>
        @foreach (var i in LanguageResourceDemo.Tools.LanguageMang.AvailableLanguages)
        {
            <li>@Html.ActionLink(i.LanguageFullName, "Index", "Language", new { lang = i.LanguageCultureName }, null)</li>
        }
    </ul>

    Here is the result.

    Best Regards,

    YihuiSun

    Wednesday, September 9, 2020 3:18 AM