Access localized resource strings without creating an instance of 'ResourceManager'?
Asked Answered
T

2

3

I have created some resource files to hold strings. I am displaying a MessageBox by pointing it directly at a resource file named TestLocalResource and a resource string named ThisIsMyTest by simply referencing the resource string like this:

TestLocalResource.ThisIsMyTest

Now this does appear to work. But this leaves me wondering that by referencing the resource string this way, will I always be accessing the default resource file (e.g., TestLocalResource.resx) rather than its German version (e.g., TestLocalResource.de-DE.resx)?

So instead, do I have to use ResourceManager? If so, how does ResourceManager know the current language (culture)? Will I need to explicitly set it?

This is how I think I would need to use ResourceManager:

ResourceManager resmgr =
    new ResourceManager("MyApplication.MyResource", Assembly.GetExecutingAssembly()); 

But I'm hoping that there is a way to access localized resource strings in a more convenient fashion.

Truncation answered 9/3, 2011 at 13:43 Comment(0)
P
11

You don't need to use the ResourceManager explicitly.
Have a look here: http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx
To learn how to change the culture to use during runtime, see the second comment in the link:

switch (comboBox1.Text)
{
    case "neutral":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("");
        break;
    case "en-GB":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
        break;
    case "de-DE":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
        break;
}

string messageText = Messages.MsgSampleText;
MessageBox.Show(messageText); 
Pyrophyllite answered 9/3, 2011 at 13:47 Comment(1)
It is important to note that the approach outlined in the referenced article and in your answer assumes the use of Visual Studio. I inspected Messages.Designer.cs (which is created by Visual Studio along with Messages.resx) and noted that it incorporates calls to ResourceManager. So, ResourceManager is indeed involved but the programmer does not need to access an instance of that class explicitly.Ciliata
C
1

In the article referenced by @Daniel Hilgarth in his answer, the author indicates that the resource files named Messages.resx and Images.resx should each be created in the Properties folder. (The Properties folder is also the location where Visual Studio places its general-purpose resource file named Resources.resx.)

I found that Visual Studio 2013 would not allow me to create and place Messages.resx in the Properties folder by right-clicking the Properties node in Solution Explorer. Instead, here's what I did:

  1. In Solution Explorer, right-click the Project node.
  2. Select Add => New Item... => Resources File => Messages.resx. Result: Visual Studio places Messages.resx in the Project folder.
  3. In Solution Explorer, drag Messages.resx to the Properties node.

As @Daniel indicates, a new class named Messages will now conveniently provide you with access to your resource strings. Note that Messages will be accessible via the YourProject.Properties namespace (where YourProject is the name of your project as shown in Solution Explorer).

For example, let's say that you have a resource string named OperationSucceeded. You will now be able to access that resource string via:

MessageBox.Show(YourProject.Properties.Messages.OperationSucceeded);

...or, more conveniently:

using YourProject.Properties;

MessageBox.Show(Messages.OperationSucceeded);
Ciliata answered 6/1, 2017 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.