The Carpool goes global
December 14, 2010 Leave a comment
I received an email from someone who wants to have a carpool website in Albanian. I hadn’t worked on globalization issues (except for hello worlds for my exam preps) so it seems like a good way to get my feet wet in .NET globalization in a real-world app.
My 1st step was FxCop. I deleted all of my FxCop suppressed messages for globalization. I got four errors like this:
CA1305 : Microsoft.Globalization : Because the behavior of ‘DateTime.Parse(string)’ could vary based on the current user’s locale settings, replace this call in ‘CarpoolSummaryFactory.GetCurrentWeekCarpoolSummaries()’ with a call to ‘DateTime.Parse(string, IFormatProvider)’. If the result of ‘DateTime.Parse(string, IFormatProvider)’ will be based on input from the user, specify ‘CultureInfo.CurrentCulture’ as the ‘IFormatProvider’ parameter. Otherwise, if the result will based on input stored and accessed by software, such as when it is loaded from disk or from a database, specify ‘CultureInfo.InvariantCulture’.
I replaced all instances of this
startDate = DateTime.Parse(DateTime.Now.ToShortDateString() );
With this
startDate = DateTime.Parse(DateTime.Now.ToShortDateString(), CultureInfo.CurrentCulture );
I then had to fix 1 of my ToString functions
public override string ToString() { return String.Format("{0}-{1}", Id, Name); }
To this
public override string ToString() { return String.Format(CultureInfo.CurrentCulture, "{0}-{1}", Id, Name); }
I then ran FxCop on the UI project (it is MVC2): I first changed the settings on code analysis to only look at Globalization.
I got 11 errors. 9 were the same as before – DateTime.Parse and 2 were Int.Parse. All of the errors were in the controller where I was taking data out of the view model and jamming it into the POCOs. I added the CultureInfo parameter to the parse function to give me a clean FxCop report.
I ran all of unit tests – all green.
I then took a look through MSDN on Globalization.
Before refactoring and adding resources files, it seemed like a good time to implement a more mature branching strategy using TFS. That is the subject of my next blog post.