Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Using Localization Backends

By default, Boost.Locale uses ICU for all localization and text manipulation tasks. This is the most powerful library available, but sometimes we don't need the full power of this library or we want to reduce dependencies from third-party libraries, and ICU is by no means a small library.

Boost.Locale provides an option to use non-ICU based localization backends. Although usually less powerful, these often provide all you need: message formatting, currency, date, time, number formatting, basic collation and case manipulation. They are implemented using the standard OS API or a C or C++ library.

When to use non-ICU backends

There are situations when using non-ICU based localization is appropriate:

Non-ICU Backends

All of the alternate backends have these limitations:

std - The standard C++ library backend

This localization backend is based on the standard C++ library.

It is supported on all platforms, but is only actually useful on platforms where the standard library supports locales besides "C" and "POSIX": on Linux with GCC or Intel compilers, and under the MSVC compiler.

It works around some common standard library bugs like invalid UTF-8 generation for numeric formatting, and it gives otherwise-absent POSIX locales names and UTF-8 support under MSVC.

It is very useful when the compiler and the library actually give fine localization support, like GCC under Linux or MSVC under Windows.

posix - POSIX 2008 C library

This backend is based on the latest POSIX 2008 standards, and uses POSIX api functions like newlocale, freelocale, strftime_l etc. It is available on the Linux and Mac OS X platforms.

It gives you simple and ready-made localization support, most notably under Mac OS X where GCC's libstdc++ does not support locales.

The POSIX backend only supports UTF-8, single-byte, and double-byte encodings.

winapi - Win32 API.

The Win32API-based localization backend provides decent UTF-8/UTF-16 locale support. It is based on Windows API functions like GetLocaleInfoW, LCMapStringW, GetDateFormatW etc and provides good localization support even on the MinGW and Cygwin platforms, which normally have problems with this.


Supported Features

Backend icuposixwinapistd
Message Formatting YesYesYesYes
Non UTF-8 encodings YesYesNoYes
Date/Time Formatting/Parsing YesFormatting OnlyFormatting OnlyFormatting Only
Monetary Formatting/Parsing YesFormatting OnlyFormatting OnlyYes
Number Formatting/Parsing YesYesYesYes
Numbers as Percent, Spelled Out YesNoNoNo
Case Manipulation YesBasicBasicBasic
Collation FullLinux - 1 level
Mac OS X - broken
3 levels1 level
Calendar YesGregorian OnlyGregorian OnlyGregorian Only
Boundary Analysis YesNoNoNo
Unicode Normalization YesNoVista and aboveNo
C++0x characters YesNoNoYes
OS Support AnyLinux, Mac OS XWindows, CygwinAny
Useful on Any PlatformLinux and Mac OS XWindows/MinGW/CygwinLinux with GCC or Intel
Windows with MSVC

Using Localization Backends

Accessing a localization backend is done via the boost::locale::localization_backend_manager class.

You can create your own boost::locale::localization_backend_manager by starting with a global backend via the boost::locale::localization_backend_manager::global static member function and modifying it.

For example:

    localization_backend_manager my = localization_backend_manager::global(); 
    // Get global backend"std"); 
    // select std backend as default

    generator gen(my); 
    // create a generator that uses this backend.

    // set this backend globally

    generator gen2();
    // now this one would use the new global backend.

You can also create a mixture of several backends, using for example icu for one kind of operation and std for all others:

    localization_backend_manager my = localization_backend_manager::global(); 
    // Get global backend"std"); 
    // select std backend as default for all categories"icu",boundary_facet); 
    // select icu backend for boundary analysis (since it is not supported by \c std)