Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting resource value with explicit localization

With different resource files (*.resx), how can I retrieve localized values by giving explicit localization.

That is, normally I can directly reference the attribute with custom-tool-namespace.Resource.localizedAttribute.

The value it will give depends on what localization has been set to CurrentCulture (thread-wise). But unlike this, I'd like to hand the localization to the resource getter. Is this possible?

like image 745
egaga Avatar asked Apr 01 '11 13:04

egaga


People also ask

Which control can you use to apply localization?

Localizing Static Text If a page includes static text, you can use ASP.NET localization by including it in a Localize control, and then using explicit localization to set the static text. The Localize control renders no markup; its only function is to act as a placeholder for localized text.

How ASP.NET applications are localized?

App localization involves the following: Make the app's content localizable. Provide localized resources for the languages and cultures you support. Implement a strategy to select the language/culture for each request.


2 Answers

Assuming you have multiple resource files:

Messages.resx
Messages.fr-FR.resx
...
Messages.xx-XX.resx

all containing some string value you could retrieve the value for a specific culture:

var culture = new CultureInfo("fr-FR");
string value = Messages.ResourceManager.GetString("SomeKey", culture);

and this will be independently of the value of the current thread culture.

like image 96
Darin Dimitrov Avatar answered Sep 21 '22 11:09

Darin Dimitrov


Better practice is to use nameof to mantain intellisense and avoid typing errors

var culture = new CultureInfo("fr-FR");
string value = Messages.ResourceManager.GetString(nameof(Messages.SomeKey), culture);
like image 33
Uraitz Avatar answered Sep 22 '22 11:09

Uraitz