34

I am working on a localised C#.NET application and we are using a strings.resx file to translate hardcoded strings in the application. I use the following code to extract them:

using MyNamespace.Resources

...

string someString = strings.someString;

But, now I want to be able to define the name of the string in the call, something like this:

string someString = GetString("someString");

I have been toying a little with the ResourceManager, but i can't find a way to direct it to my strings.resx file.

How do I do that?

5 Answers 5

54

A little searching did the trick. I have the right ResourceManager available in my strings class:

ResourceManager rm = strings.ResourceManager;
string someString = rm.GetString("someString");
Sign up to request clarification or add additional context in comments.

Comments

23

ResourceManager.GetString should do.

Stripped down example from MSDN:

ResourceManager rm = new ResourceManager("RootResourceName",
                                         typeof(SomeClass).Assembly);
string someString = rm.GetString("someString");

4 Comments

Yes, I found that, but now idea how I should instantiate my ResourceManager. I got it working with ResourceManager rm = strings.ResourceManager;. No need to instantiatie: it's already there.
@Bart: Did you try the constructor with assembly?
yes, but I didn't know what "RootResourceName" or SomeClass should be. The strings.ResourceManager is way easier.
@Bart: class is needed only in order to get to the assembly. You could perhaps use just Assembly.GetCurrentAssembly or like that. And RootResourceName is, according to the docs, "The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named MyApplication.MyResource.en-US.resources is MyApplication.MyResource." However, if the resource manager is already available, it's better to take it as is.
12

I had the same problem using ASP.NET Core MVC and managed to solve it using

ResourceManager rm = new ResourceManager(typeof(YourResourceClass));
string someString = rm.GetString("someString");

Very similar to @Vlad's solution, but otherwise I had a MissingManifestResourceException

Comments

10

There is much simpler way of doing this

 [NameOfyourResxfile].ResourceManager.GetString("String Name");

in your case

strings.resx.ResourceManager.GetString("someString");

Comments

5

You can write a static method like this:

public static string GetResourceTitle<T>(string key)
{
  ResourceManager rm = new ResourceManager(typeof(T));
  string someString = rm.GetString(key);
  return someString;
}

And call anywhere:

var title=  GetResourceTitle<*YouResourceClass*>(key);

It is useful when you want to have a generic function to get String of any Resource file.

1 Comment

Very nice solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.