Assume that you are getting the language/region identifier from an external source. What you want is to format numbers, dates and other locale sensitive information. The most natural way to do so is to
// assume lang_id is a std::string
std::ostream out;
out.imbue(std::locale(lang_id.c_str()));
out << data << std::ends;
// etc

This code also assumes that the string lang is in the form specified by RFC1766, so the United States English would be en_US.

If you try to use this with Visual Studio 2003 C++ compiler (and it's standard library) you will get an exception stating that the language was not recognized. If you debug the code, you will see that locale implementation essentially calls C library setlocale and that the parsing code for the language/region is extremely convoluted and does a lot of guesswork. What it doesn't do though is accept proper identifiers. Sigh.

Unsurprisingly this non-conformance is rectified in the .NET Framework library. The identifiers there are of the form en-US (interestingly the name used is now culture and not locale any more) but that conforms to the newer RFC3282 anyway.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
0 Comments