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!