egaga
egaga

Reputation: 21722

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?

Upvotes: 15

Views: 8026

Answers (2)

Uraitz
Uraitz

Reputation: 496

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);

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 31

Related Questions