Sunday, May 20, 2012

Istanbul (Not Constantinople)

Chapter five of C# 4.0 in a Nutshell goes into globalization and localization and illustrates that the local culture is driven from the control panel of the PC at hand (or I suppose from elsewhere on other operating systems) and that one may overpower this setting in code as shown below. The book suggests that Turkey is a good country to use when testing if what you are doing supports globalization. DateTime values are formatted wildly differently from the norm and when using .ToUpper() in lieu of .ToUpperInvariant() there can be some more wackiness. The tests below pass.

using System;
using System.Globalization;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Whatever.Tests
{
   [TestClass]
   public class ThreadTest
   {
      [TestMethod]
      public void TestForTexas()
      {
         string currentCulture = Thread.CurrentThread.CurrentCulture.TextInfo.ToString();
         DateTime today = new DateTime(2012, 5, 20);
         Assert.AreEqual(currentCulture, "TextInfo - en-US");
         Assert.AreEqual(today.ToString(), "5/20/2012 12:00:00 AM");
         Assert.AreEqual("i".ToUpper(), "I");
         Assert.AreEqual("I".ToLower(), "i");
      }
      
      [TestMethod]
      public void TestForTurkey()
      {
         Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("tr-TR");
         string currentCulture = Thread.CurrentThread.CurrentCulture.TextInfo.ToString();
         DateTime today = new DateTime(2012, 5, 20);
         Assert.AreEqual(currentCulture, "TextInfo - tr-TR");
         Assert.AreEqual(today.ToString(), "20.05.2012 00:00:00");
         Assert.AreEqual("i".ToUpper(), "İ");
         Assert.AreEqual("I".ToLower(), "ı");
         Assert.AreEqual("i".ToUpperInvariant(),"I");
         Assert.AreEqual("I".ToLowerInvariant(), "i");
      }
   }
}

No comments:

Post a Comment