Resource Files In A Console Application

I am following this article about Locating and Using Resources for a Specific Culture.

I created a new Console Application. I then added a new default resource file with the following content:

image

The resource file was placed in the properties folder:

image

I then renamed the file Greeting.resx. The problem is that VS2010 now thinks that there is not a default resource file:

image

I decided to follow the article and not the default behavior of VS2010 so I pressed on without a default resource file.

I then added new resource files:

image

I then went to the AssesmblyInfo file and added the following entry:

[assembly: NeutralResourcesLanguageAttribute("en")]

Unfortunately, it is not recognized until I added a reference to System.Resources. (I looked in System.Globalization first – ooops).

I then was ready to start coding the different cultures.

I first added the System.Globalization, System.Threading,and System.Reflection namespaces and wrote a quick function to see the different cultureInfo:

static void Main(string[] args) { CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; Console.WriteLine("The current CultureInfo is {0}", cultureInfo.EnglishName); Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR"); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; ResourceManager resourceManager = new ResourceManager("Com.Tff.CultureInfo", Assembly.GetExecutingAssembly()); Console.WriteLine("The current CultureInfo is {0}: {1}", cultureInfo.EnglishName, resourceManager.GetString("HelloString")); Console.ReadKey(); }

and got the following error:

image

Hum, it can’t find the resource file. I wondered if it was the spelling of the assembly in the constructor of Resource Manager. I changed it to match the Assembly name:

ResourceManager resourceManager = new ResourceManager("Com.Tff.CultureDemo", Assembly.GetExecutingAssembly());

 

image

 

It still failed so I went on MSDN and looked at the help on this exception here. That article had a different project so I fired up another instance of VS2010. Following the instructions, I created a new ASP.Net web project and added the following code the Page_Load of default.aspx:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja"); ResourceManager resourceManager = new ResourceManager("Com.Tff.CultureTest.Strings", Assembly.Load("Com.Tff.CultureTest")); this.IntroLabel.Text = String.Format("Hello {0}", resourceManager.GetString("HelloString"));

I also added a resource file called Strings.ja.resx to the web site’s root. I hit F5 and things worked:

image

What I learned though induction is that Resource files in VS2010 depend on convention:

The CultureInfo class associates with the xx in the yyyy.xx.resx file.

The ResourceManager class associates with the yyyy in the yyyy.xx.resx file.

The rest of the tutotial deals with creating a satellite assembly. I am not going to do that yet – I am going back to my original solution to see if I could get the resource file located in a Console application.

I changed the ResourceManager constructor to this:

ResourceManager resourceManager = new ResourceManager("Com.Tff.CultureDemo.Greeting", Assembly.GetExecutingAssembly());

And boom goes the dynamite:

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: