Globalizing the carpool app

I had a question about how to internationalize the carpool app.  I ran through the exercise to try out the Globalization and Localization features of the .NET framework.  

I 1st I added a couple of resource files:

image

Next, I added a simple string to test:

image

Next, I added a reference in the markup on the site.master:

<div id="title"> <h1> <asp:Label ID="LabelMainTitle" runat="server" Text="<%$ Resources:LocalizedText, SiteTitle %>"></asp:Label> </h1> </div>

Next, I updated the web.config file:

<globalization enableClientBasedCulture="true" uiCulture="auto" />

Finally, I changed the browser settings:

image

And Boom goes the dynamite:

image

So the next step is adding the strings to the resource file…

I am using this site for translations.

A couple of random thoughts about the process:

1) ActionLink – need to refer to the class explicitly:

<ul id="menu"> <li><%: Html.ActionLink(Resources.LocalizedText.HomeTab, "Index", "Home")%></li> <li><%: Html.ActionLink(Resources.LocalizedText.PracticeTab , "Index", "Practice")%></li> <li><%: Html.ActionLink(Resources.LocalizedText.AboutTab, "About", "Home")%></li> </ul>

And I don’t know why no inteliisense from the namespace to class, but there is intliisense from the class to the property name.

2) I appreciate the split screen feature of VS2010:

image

3) I ran into a problem with a new HTML Helper Controls in MVC:

<%: Html.LabelFor(model => model.Date) %>

This is how it renders:

image

Apparently, the LabelFor does not support globalization in the view. That means I would need to add the LocalizedDisplayName attribute to each of the POCOs as recommend here OR remove the LabelFor and hand code  labels. Since I am not interested in creating something outside of the base MVC API, I split out the labels like so:

From This:

<%: Html.LabelFor(model => model.Date) %>

 

To This:

<%: Html.Label(Resources.LocalizedText.DateHeader)%>

After spending a couple of hours associating text to the resource files, I was done

Here is the login screen:

image

And the main page:

image

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