Boost.Locale
Design Rationale

Why do I need Boost.Locale?

Why do we need a localization library, when standard C++ facets (should) provide most of the required functionality?

  • Case conversion is done using the std::ctype facet
  • Collation is supported by std::collate and has nice integration with std::locale
  • There are std::num_put , std::num_get , std::money_put , std::money_get , std::time_put and std::time_get for numbers, time, and currency formatting and parsing.
  • There is a std::messages class that supports localized message formatting.

So why do we need such a library if we have all the functionality within the standard library?

Almost every(!) facet has design flaws:

  • std::collate supports only one level of collation; it does not allow you to choose whether case- or accent-sensitive comparisons should be performed.
  • std::ctype, which is responsible for case conversion, assumes that all conversions can be done on a per-character basis. This is probably correct for many languages but it isn't correct in general.
    1. Case conversion may change a string's length. For example, the German word "grüßen" should be converted to "GRÜSSEN" in upper case: the letter "ß" should be converted to "SS", but the toupper function works on a single-character basis.
    2. Case conversion is context-sensitive. For example, the Greek word "ὈΔΥΣΣΕΎΣ" should be converted to "ὀδυσσεύς", where the Greek letter "Σ" is converted to "σ" or to "ς", depending on its position in the word.
    3. Case conversion cannot assume that a character is a single code point, which is incorrect for both the UTF-8 and UTF-16 encodings, where individual code-points are represented by up to 4 char 's or two wchar_t 's on the Windows platform. This makes std::ctype totally useless with these encodings.
  • std::numpunct and std::moneypunct do not specify the code points for digit representation at all, so they cannot format numbers with the digits used under Arabic locales. For example, the number "103" is expected to be displayed as "١٠٣" in the ar_EG locale.
    std::numpunct and std::moneypunct assume that the thousands separator is a single character. This is untrue for the UTF-8 encoding where only Unicode 0-0x7F range can be represented as a single character. As a result, localized numbers can't be represented correctly under locales that use the Unicode "EN SPACE" character for the thousands separator, such as Russian.
    This actually causes real problems under GCC and SunStudio compilers, where formatting numbers under a Russian locale creates invalid UTF-8 sequences.
  • std::time_put and std::time_get have several flaws:
    1. They assume that the calendar is always Gregorian, by using std::tm for time representation, ignoring the fact that in many countries dates may be displayed using different calendars.
    2. They always use a global time zone, not allowing specification of the time zone for formatting. The standard std::tm doesn't even include a timezone field at all.
    3. std::time_get is not symmetric with std::time_put, so you cannot parse dates and times created with std::time_put . (This issue is addressed in C++11 and some STL implementation like the Apache standard C++ library.)
  • std::messages does not provide support for plural forms, making it impossible to correctly localize such simple strings as "There are X files in the directory".

Also, many features are not really supported by std::locale at all: timezones (as mentioned above), text boundary analysis, number spelling, and many others. So it is clear that the standard C++ locales are problematic for real-world applications.

Why use an ICU wrapper instead of ICU?

ICU is a very good localization library, but it has several serious flaws:

  • It is absolutely unfriendly to C++ developers. It ignores popular C++ idioms (the STL, RTTI, exceptions, etc), instead mostly mimicking the Java API.
  • It provides support for only one kind of string, UTF-16, when some users may want other Unicode encodings. For example, for XML or HTML processing UTF-8 is much more convenient and UTF-32 easier to use. Also there is no support for "narrow" encodings that are still very popular, such as the ISO-8859 encodings.

For example: Boost.Locale provides direct integration with iostream allowing a more natural way of data formatting. For example:

cout << "You have "<<as::currency << 134.45 << " in your account as of "<<as::datetime << std::time(0) << endl;

Why an ICU wrapper and not an implementation-from-scratch?

ICU is one of the best localization/Unicode libraries available. It consists of about half a million lines of well-tested, production-proven source code that today provides state-of-the art localization tools.

Reimplementing of even a small part of ICU's abilities is an infeasible project which would require many man-years. So the question is not whether we need to reimplement the Unicode and localization algorithms from scratch, but "Do we need a good localization library in Boost?"

Thus Boost.Locale wraps ICU with a modern C++ interface, allowing future reimplementation of parts with better alternatives, but bringing localization support to Boost today and not in the not-so-near-if-at-all future.

Why is the ICU API not exposed to the user?

Yes, the entire ICU API is hidden behind opaque pointers and users have no access to it. This is done for several reasons:

  • At some point, better localization tools may be accepted by future upcoming C++ standards, so they may not use ICU directly.
  • At some point, it should be possible to switch the underlying localization engine to something else, maybe the native operating system API or some other toolkit such as GLib or Qt that provides similar functionality.
  • Not all localization is done within ICU. For example, message formatting uses GNU Gettext message catalogs. In the future more functionality may be reimplemented directly in the Boost.Locale library.
  • Boost.Locale was designed with ABI stability in mind, as this library is being developed not only for Boost but also for the needs of the CppCMS C++ Web framework.

Why use GNU Gettext catalogs for message formatting?

There are many available localization formats. The most popular so far are OASIS XLIFF, GNU gettext po/mo files, POSIX catalogs, Qt ts/tm files, Java properties, and Windows resources. However, the last three are useful only in their specific areas, and POSIX catalogs are too simple and limited, so there are only two reasonable options:

  1. Standard localization format OASIS XLIFF.
  2. GNU Gettext binary catalogs.

The first one generally seems like a more correct localization solution, but it requires XML parsing for loading documents, it is very complicated format, and even ICU requires preliminary compilation of it into ICU resource bundles.

On the other hand:

  • GNU Gettext binary catalogs have a very simple, robust and yet very useful file format.
  • It is at present the most popular and de-facto standard localization format (at least in the Open Source world).
  • It has very simple and powerful support for plural forms.
  • It uses the original English text as the key, making the process of internationalization much easier because at least one basic translation is always available.
  • There are many tools for editing and managing gettext catalogs, such as Poedit, kbabel etc.

So, even though the GNU Gettext mo catalog format is not an officially approved file format:

  • It is a de-facto standard and the most popular one.
  • Its implementation is much easier and does not require XML parsing and validation.
Note
Boost.Locale does not use any of the GNU Gettext code, it just reimplements the tool for reading and using mo-files, eliminating the biggest GNU Gettext flaw at present – thread safety when using multiple locales.

Why is a plain number used for the representation of a date-time, instead of a Boost.DateTime date or Boost.DateTime ptime?

There are several reasons:

  1. A Gregorian Date by definition can't be used to represent locale-independent dates, because not all calendars are Gregorian.
  2. ptime – definitely could be used, but it has several problems:
    • It is created in GMT or Local time clock, when time() gives a representation that is independent of time zones (usually GMT time), and only later should it be represented in a time zone that the user requests.
      The timezone is not a property of time itself, but it is rather a property of time formatting.
    • ptime already defines operator<< and operator>> for time formatting and parsing.
    • The existing facets for ptime formatting and parsing were not designed in a way that the user can override. The major formatting and parsing functions are not virtual. This makes it impossible to reimplement the formatting and parsing functions of ptime unless the developers of the Boost.DateTime library decide to change them.
      Also, the facets of ptime are not "correctly" designed in terms of division of formatting information and locale information. Formatting information should be stored within std::ios_base and information about locale-specific formatting should be stored in the facet itself.
      The user of the library should not have to create new facets to change simple formatting information like "display only the date" or "display both date and time."

Thus, at this point, ptime is not supported for formatting localized dates and times.

Why are POSIX locale names used and not something like the BCP-47 IETF language tag?

There are several reasons:

  • POSIX locale names have a very important feature: character encoding. When you specify for example fr-FR, you do not actually know how the text should be encoded – UTF-8, ISO-8859-1, ISO-8859-15 or maybe Windows-1252. This may vary between different operating systems and depends on the current installation. So it is critical to provide all the required information.
  • ICU fully understands POSIX locales and knows how to treat them correctly.
  • They are native locale names for most operating system APIs (with the exception of Windows)

Why do most parts of Boost.Locale work only on linear/contiguous chunks of text?

There are two reasons:

  • Boost.Locale relies heavily on the third-party APIs like ICU, POSIX or Win32 API, all of them work only on linear chunks of text, so providing non-linear API would just hide the real situation and would hurt performance.
  • In fact, all known libraries that work with Unicode: ICU, Qt, Glib, Win32 API, POSIX API and others accept an input as single linear chunks of text and there is a good reason for this:
    1. Most supported operations on text like collation, case handling usually work on small chunks of text. For example: you probably would never want to compare two chapters of a book, but rather their titles.
    2. We should remember that even very large texts require quite a small amount of memory, for example the entire book "War and Peace" takes only about 3MB of memory.
      However:
  • There are API's that support stream processing. For example: character set conversion using std::codecvt API works on streams of any size without problems.
  • When new API is introduced into Boost.Locale in future, such that it likely works on large chunks of text, will provide an interface for non-linear text handling.

Why is all Boost.Locale implementation hidden behind abstract interfaces instead of using template metaprogramming?

There are several major reasons:

  • This is how the C++'s std::locale class is build. Each feature is represented using a subclass of std::locale::facet that provides an abstract API for specific operations it works on, see Introduction to C++ Standard Library localization support.
  • This approach allows to switch underlying API without changing the actual application code even in run-time depending on performance and localization requirements.
  • This approach reduces compilation times significantly. This is very important for library that may be used in almost every part of specific program.

Why doesn't Boost.Locale provide char16_t/char32_t for non-C++11 platforms?

There are several reasons:

  • C++11 defines char16_t and char32_t as distinct types, so substituting it with something like uint16_t or uint32_t would not work as for example writing uint16_t to uint32_t stream would write a number to stream.
  • The C++ locales system would work only if standard facets like std::num_put are installed into the existing instance of std::locale, however in the many standard C++ libraries these facets are specialized for each specific character that the standard library supports, so an attempt to create a new facet would fail as it is not specialized.

These are exactly the reasons why Boost.Locale fails with current limited C++11 characters support on GCC-4.5 (the second reason) and MSVC-2010 (the first reason)

Basically it is impossible to use non-C++ characters with the C++'s locales framework.

The best and the most portable solution is to use the C++'s char type and UTF-8 encodings.