1

TL;TR: Using multi language resources, class library keeps using server language instead if user decided language (url parameter).

Environment: ASP.NET 4.5.2, Visual Studio 2015 RC.

So I have pretty big website setup in multi language, using Resources. I have a separate project in my solution that only contains the .resx files for two languages. Default language is Dutch, second is English, so the resource files are named: Resources.resx and Resources-en.resx.

I determine the language by the url, I have a Localization attribute, it is added in the FilterConfig and is used in the MVC routing. (link below)

So website example.com/ will give dutch, example.com/en will give english.

There are two more projects in the solution. One class library, one MVC application and the previous mentioned Language project.

This all works pretty well, I can call Resources.Resources.xx from every point I want. All models are inside the class library and they also have multi language DataAnnotations which work correctly in both languages, I can throw exceptions with multi language error message which also comes in the correct language.

Yet I have one issue (else I wasn't here, right?).

Based on selecting some products, additional costs are added to the shopping cart. These are dynamically added to the shopping cart and saved in the database. The description and title for these additional costs also use these resources, yet they always come in English (not the selected language!). This happens inside the class library, and depending on the server language, it uses that resource language.

Same goes for generating Invoices, where it also uses the Resources project, it as well always use the server language to determine what text to get. When I test locally (on a computer language set to Dutch) it gives the additional costs description in dutch and invoice description in dutch as well, but when choosing the english language, it still gives everything in dutch, so it keeps the server language.

So you want some code?

Used this blog post to get the localization setup (not advertising here, not mine)

So on every request, this code is executed:

            CultureInfo info = new CultureInfo(lang);//nl-NL or en-EN
            info.NumberFormat = numberInfo;
            info.DateTimeFormat.DateSeparator = "/";
            info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
                Thread.CurrentThread.CurrentUICulture = info;
            Thread.CurrentThread.CurrentCulture = info;

Little bit of code:

AdditionalCost adcost = new AdditionalCost()
{
    //unrelated stuff here
    title = Resources.Resources.additionalCostsDepositTitle,
    description = Resources.Resources.additionalCostsDepositDescription,
    //unrelated stuff here
};

when I get the CultureInfo.CurrentCulture it is nl-NL even though the entire website is in english (because the current url is example.com/en).

Hope anyone got some experience with setting up your class libraries to use multi language as well (for setting database attributes). I have shared hosting, so I cannot control the server, but it has to be multi language depending on the users choice anyway. Thanks!

CularBytes
  • 9,924
  • 8
  • 76
  • 101
  • Are you sure the `Thread.CurrentThread.CurrentCulture` and `Thread.CurrentThread.CurrentUICulture` are correctly set _before_ you enter the code that you pasted? – Mark Feb 06 '16 at 18:50
  • See updated answer, Current Culture is server language. – CularBytes Feb 06 '16 at 19:02
  • maybe the bug is how you determine `lang`? – NikolaiDante Feb 06 '16 at 19:03
  • For the website it is determined by the URL, how asp.net determines it when using such Resources? i dont know – CularBytes Feb 06 '16 at 19:05
  • The thread setting looks ok (assuiming its on `Application_BeginRequest` or similar`, the bit we can't see is how you set the `lang` variable. – NikolaiDante Feb 06 '16 at 19:09
  • The lang variable is set by the url Language parameter, if the url is example.com/en it is converted to en-GB, and when debugging that code, it hits on every get or post request. – CularBytes Feb 06 '16 at 19:15
  • 1
    My bet: you're using multiple threads and you only set the culture for the current thread. Try this: https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.defaultthreadcurrentculture%28v=vs.110%29.aspx – Gusman Feb 06 '16 at 19:39
  • Please provide a [Minimal, Complete, and Verifyable Example](http://stackoverflow.com/help/mcve). It is unclear from your question *where* the code you pasted is being run. – NightOwl888 Feb 06 '16 at 21:10
  • Note that you should not use `ActionFilterAttribute` as the post suggests because this will not enable localization in the model binder. You should use `IAuthorizationFilter` as in [this answer](http://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url#32839796). – NightOwl888 Feb 06 '16 at 21:10
  • Thanks for your help guys, @Gusman you were right, it was executed on another thread. – CularBytes Feb 07 '16 at 09:59

0 Answers0