0

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.

Maksym Labutin
  • 561
  • 1
  • 8
  • 17
saleem
  • 295
  • 2
  • 7
  • 15

1 Answers1

0

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);
Artavazd
  • 132
  • 1
  • 6
  • i try to use cookies and it's has the same problem , when i work on single user it's work perfectly , i think there is a RaceCondition problem in resource manager that cause loading resources with invalid UI Culture for current thread ! – saleem Dec 06 '18 at 12:49