33

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?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

5 Answers5

52

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");
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
23

ResourceManager.GetString should do.

Stripped down example from MSDN:

ResourceManager rm = new ResourceManager("RootResourceName",
                                         typeof(SomeClass).Assembly);
string someString = rm.GetString("someString");
Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 1
    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 Friederichs Nov 02 '12 at 11:35
  • 1
    @Bart: Did you try the [constructor with assembly](http://msdn.microsoft.com/en-us/library/yfsz7ac5.aspx)? – Vlad Nov 02 '12 at 11:36
  • 2
    yes, but I didn't know what `"RootResourceName"` or `SomeClass` should be. The `strings.ResourceManager` is way easier. – Bart Friederichs Nov 02 '12 at 11:38
  • 2
    @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. – Vlad Nov 02 '12 at 11:41
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

skamlet
  • 693
  • 7
  • 23
10

There is much simpler way of doing this

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

in your case

strings.resx.ResourceManager.GetString("someString");
Emil
  • 6,411
  • 7
  • 62
  • 112
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.

A.Dara
  • 784
  • 10
  • 23