简体   繁体   中英

ASP.net MVC 5 Multi-Language: language resources sometimes do not work consistently

I'm using the resource manager to load resources based on threadUI culture in an ASP.NET MVC 5 application.i set thread culture in Application_AcquireRequestState Event, current culture saves per user, it's loaded by a web server in DataBase as below Code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        string languageName = default(string);

        CultureInfo ci = default(CultureInfo);

        if (!User.Identity.IsAuthenticated)
        {
            languageName = Request.RequestContext.HttpContext.Session["_culture"] == null ?
                Helper.ApplicationInformation.AppCulture.Name :
                Request.RequestContext.HttpContext.Session["_culture"].ToString();

            ci = CultureInfo.GetCultureInfo(languageName.ToUpper());
        }
        else
        {
            ci = CultureInfo.GetCultureInfo(Helper.User.Preferences.Language.Name);
        }

        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;

        Bridge.Resources.Global.Culture = ci;
        Bridge.Resources.Login.Culture = ci;
        Bridge.Resources.Search.Culture = ci;
        Bridge.Resources.Workspace.Culture = ci;
    }

it's happening when setting the culture in a different thread, actually when 2 user or more changes languages at the same time,

I think there is a RaceCondition problem in resource manager that cause loading resources with invalid UI Culture for the current thread

I did a research about this and find the following relevant links:

ASP.Net MVC resource files are sometimes incorrectly loaded by the ResouceManager

ASP.NET MVC 5 localization resources freeze and do not change language despite changing CurrentThread.CurrentCulture

ASP.NET Resource Manager RaceCondition in Multilingual Application

I try to download example for multi-language but it also happens, I download from the following link:

MultiLanguageExample

add 'ThreadSleep(4000)' in action:index control:Home to reproduce this issue.

I did everything mentioned but nothing work. what can I try to make resources to work consistently?

thanks.

There are several ways to set each user language. IIS Server keep Sessions for 20 minutes. You need to change time in web.config file by adding row:

<sessionState timeout="120" cookieless="AutoDetect">
// Change timeout to your preferred value 

Or you need to go and change settings in Application Pools. For my project I use cookies to save user language option. Cookies set to be avaliable for very very long time. For example I've used in Global.asax Application_BeginRequest

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["NAMEOFCOOKIE"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
            }
            else
            {
               // FIND IN DB YOUR USER'S LANGUAGE AND SET IT 
                        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                        HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
                        cookieNew.Value = "YOUR USERS'S DB VALUE";
                        Response.Cookies.Add(cookieNew);
                    }
                }
            }
        }

And don't forget one thing when you are using resources you need to put it the end of filename your language short name. For example you have default file MainPageLang.resx and you need English and Russian, you have to add files MainPageLang.en.resx and MainPageLang.ru.resx.

"YOUR USERS'S DB VALUE" must be "en" or "ru".

                    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("YOUR USERS'S DB VALUE");
                    HttpCookie cookieNew = new HttpCookie("NAMEOFCOOKIE");
                    cookieNew.Value = "YOUR USERS'S DB VALUE";
                    Response.Cookies.Add(cookieNew);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM