Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

c# - Access localized resource strings without creating an instance of 'ResourceManager'?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...