Getting resource value with explicit localization
Asked Answered
M

2

15

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?

Midge answered 1/4, 2011 at 13:17 Comment(0)
N
31

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.

Nikethamide answered 1/4, 2011 at 13:33 Comment(1)
Thank you, there was so many attributes that I didn't notice the Messages-object had also ResourceManager.Midge
R
5

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);
Rheims answered 18/7, 2019 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.