Search code examples
c++formattingcross-platformlocale

Cross-platform table of locales


I was testing some of the new locale stuff provided by C++11 like std::put_money to get used to their use. I was thinking that would be nice to show a large number printed as currency for all the countries supported by the standard library:

const char *Locales[] =
{
    "English_Australia.1252",
    "en_EN",
    "fr_FR.UTF-8"
    / ...
    // lots of lots of worldwide locales
};

int main(int argc, char **argv)
{
    for (const char **Locale = std::begin(Locales); Locale != std::end(Locales); ++Locale)
    {
        std::stringstream ss;
        ss.imbue(std::locale(*Locale))

        std::cout << *Locale << ' '
                  << std::showbase << std::put_money(11223344556677889900) << '\n';
    }
}

I was shocked when realized that the locale names aren't standard at all, for my Visual Studio 10.0 under Windows 7 Professional (SP1) (Spanish locale machine) the locale names follows the format Language_Region.charsetID for example: English_Australia.1252, in my Linux machine (Debian 4.2.2) with gcc 4.2.3 the locales follows the same format but with two letters xx_XX.UTF-8 (for example es_ES.UTF-8) and while trying code on Ideone the only locale that seems to accept is the en_US.UTF-8.

The most frustrating part is to try to get a standard, cross-platform table of supported locales, the best one I've found Googling around is this one that lists locales for Windows and their equivalents in no-Windows, but lacks of lots of countries (like USA, Guatemala, Mexico...).

Another thing I've tried is to debug step-by-step the std::locale constructor with the hope of found the piece of code that says which locale name is valid and which one isn't. I was expecting a giant array of locale names (like the one in the example code) but I was frightened by the complexity of the the std::locale constructor and fled away.

So my questions are:

  • Which is the best and most complete table of cross-platform locales that you know and use?
  • How to know the locales supported by the running system? How to access to the supported locales list?

Extra questions:

  • Why the locale names aren't standard (granting a cross-platform source code)?
  • Which locale system is used by Ideone?

Solution

  • On UNIX-y systems, you get the list of locales by running locale -a, which is also recommended by man setlocale. I don't think that the C library even has a list of locales, because it looks them up at run-time in /usr/lib/locale.

    In essence, this means that you can't know the locale list at compile time, because it is a runtime feature of your target system.