Dealing with multi-tenancy (i.e. multiple domains mapping to the same application). The answer for this does depend on how the tenencies are set up. For example, if you follow the Wikipedia model, the culture name is encoded in the URL (i.e. en.wikipedia.org, ja.wikipedia.org, de.wikipedia.org). In this case you have your CultureInfo encoded into the URL itself. You just need to extract it and use it to retrieve your resource. If you have another model for multi-tenancy, then your best bet might be to use a session variable or cookie to remember the requested culture.
The remaining part of the problem is directly related to i18n (internationalization) and l10n (localization). The folks at ASP.Net have come up with a great article on how to build that infrastructure in to your system (link to article). Essentially it uses the some of the clues I provided, but wraps them up in helper classes so you always have access to it as you need it:
- They recommend using the
ASPNET_CULTURE
cookie (CookieRequestCultureProvider) to store the culture info.
- The preferred option to automatically detecting the desired culture is to use the
Accept-Language
HTTP header. When set, it provides the preferred order of cultures. This provides a better experience for expats who live in foreign countries but want to read in their native language.
- Your
.resx
files follow the standard naming pattern: (FileName.resx
for default language, FileName.fr.resx
for the French culture, etc.). The i18n variants must live in the same namespace/folder as the default .resx
file. (this is required for the ResourceManager
to resolve resources based on culture info).
- The article provides examples of creating your own helpers to look up and resolve resources automatically.
The ".resx" file has a few properties and methods that you may not know about.
- Culture -- the
CultureInfo
to use for all requests by name
- ResourceManager -- the actual dictionary for the resources
Each property you've defined in your ".resx" file simply calls the following equivalent method:
ResourceManager.GetString("ResourceName", Culture);
There's nothing that automatically looks at your Thread.CurrentCulture
values automatically. However, you can use the nameof()
operator and that value to get your values:
MyResources.ResourceManager.GetString(
nameof(MyResources.ResourceManager.ResourceName),
Thread.CurrentUICulture);