Archive

Posts Tagged ‘Globalization’

Formatting Globalized Strings

July 25th, 2010 No comments

The String.Format method is quite useful for declaring the text representation of data:

String.Format(“{0}, {1}”, person.LastName, person.FirstName)

However, when globalizing applications, we must use the overloads which accept IFormatProvider:

String.Format(

  CultureInfo.CurrentCulture,

  “{0}, {1}”,

  person.LastName,

  person.FirstName)

We can reduce the friction of these calls with extension methods:

“{0}, {1}”.FormatCurrent(person.LastName, person.FirstName)

Each known culture gets its own extension method:

public static string FormatCurrent(this string format, params object[] arguments)

{

  return String.Format(CultureInfo.CurrentCulture, format, arguments);

}

 

public static string FormatInvariant(this string format, params object[] arguments)

{

  return String.Format(CultureInfo.InvariantCulture, format, arguments);

}

 

// Other known cultures