My Development Notes

By Haemoglobin
3/10/2010 (revision 2)

Globalization

  • Thread.CurrentThread.CurrentCulture - date, number and currency formatting. Must use es-MX or fr-FR (both language and region), not just es or fr (language) i.e specific culture.
  • Thread.CurrentThread.CurrentUICulture - determines what resources are loaded by resource manager (can use es and fr on it's own) i.e neutral culture
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES"); 
DateTime.Now.ToString();
//or
DateTime.Now.ToString(new CultureInfo("en-GB"));

System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures|NeutralCultures|SpecificCultures); 

//Formatting your own data based on current culture 
ci.NumberFormat.NumberDecimalSeparator
ci.NumberFormat.NumberGroupSizes
ci.NumberFormat.NumberGroupSeparator
ci.NumberFormat.CurrencySymbol .... etc

ci.DateTimeFormat.GetDayName
ci.DateTimeFormat.AMDesignator 
ci.DateTimeFormat.DateSeparator
ci.DateTimeFormat.TimeSeparator ... etc

Changing the culture can affect sort order (e.g when using Array.Sort). i.e special symbols can be used in different languages but appear at different places in their alphabet.

For internal operations where different cultures could cause problems, specify CultureInfo.InvariantCulture or where needed for example

s.IndexOf(symbol, StringComparison.InvariantCulture); 
//or
CompareInfo comp = new CultureInfo("en-NZ").CompareInfo; 
comp.IndexOf(symbol, stringToSearch); 

Building a custom culture

CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("en-CU", CultureAndRegionModifiers.None);
cib.LoadDataFromCultureInfo(new CultureInfo("en-US")); 
cib.LoadDataFromRegionInfo(new RegionInfo("US")); 

//Override custom settings
cib.CultureEnglishName = "Funny english"; 
cib.ISOCurrentSymbol = "*"; 
//Register (need admin rights) 
cib.Register(); 

CultureInfo ci = new CultureInfo("en-CU"); 

Encoding

Code page that translates a number to a character (different ones for different languages)

UTF-X are UNICODE (massive code page for all languages)

ASCII7-bit byte 0 - 127
ANSI Standards8 bit byte, different code pages for 128-255
UTF-3232 bit integers
UTF-16.NET default
UTF-8Uses 8, 16, 24 -> 48 to support languages as needed
StreamReader sw = new StreamReader(fileName, Encoding.UTF7, false )
Encoding e = Encoding.GetEncoding("korean"); 
e.GetBytes("hello"); 
StreamWriter sw = new StreamWriter(fileName, Encoding.UTF7, false )

Comments

Powered by BlogEngine.NET 1.6.1.0 | Design by styleshout | Enhanced by GravityCube.net | 1.4.5 Changes by zembian.com | Adapted by HamishGraham.NET
(c) 2010 Hamish Graham. Banner Image (c) Chris Gin