Skip to content
Search

Boost:Locale

lastmod 2025-02-14created 2015-03-2615 edits

Boost.Locale is a library that provides high quality localization facilities in a C++ way. It was originally designed a part of CppCMS - C++ Web Framework project and then contributed to Boost.

Boost.Locale gives powerful tools for development of cross platform localized software - the software that talks to user in its language.

Provided Features

  • Correct case conversion, case folding and normalization.
  • Collation (sorting), including support for 4 Unicode collation levels.
  • Date, time, timezone and calendar manipulations, formatting and parsing, including transparent support for calendars other than Gregorian.
  • Boundary analysis for characters, words, sentences and line-breaks.
  • Number formatting, spelling and parsing.
  • Monetary formatting and parsing.
  • Powerful message formatting (string translation) including support for plural forms, using GNU catalogs.
  • Character set conversion.
  • Transparent support for 8-bit character sets like Latin1
  • Support for char and wchar_t
  • Experimental support for C++0x char16_t and char32_t strings and streams.

Translation Example

First, write the locale/en/LC_MESSAGES/messages.po file and the locale/ko_KR/LC_MESSAGES/messages.po file. The code below is the content for ko_KR.

msgid ""
msgstr ""
"Project-Id-Version: VTEST 0.3\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-05-07 14:02+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: yourname <[email protected]>\n"
"Language-Team: yourname <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "HelloWorld"
msgstr "안녕, 세상!"

(Note that for en, msgstr "Hello, World!" was entered instead of msgstr "안녕, 세상!".)

You can test it as follows.

#include <boost/locale.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <iostream>
// ...
boost::locale::generator gen;

// Specify location of dictionaries
gen.add_messages_path((boost::filesystem::current_path() / "locale").string());
gen.add_messages_domain("messages");

// Generate locales and imbue them to iostream
std::locale::global(gen(""));
std::cout.imbue(locale());
// Display a message using current system locale
std::cout << boost::locale::translate("HelloWorld") << std::endl;

// OR

std::locale en_loc = gen.generate("en.UTF-8");
std::locale ko_loc2 = gen.generate("ko_KR.UTF-8");
std::string system_string = boost::locale::translate("HelloWorld");
std::string en_string = boost::locale::translate("HelloWorld").str(en_loc);
std::string ko_string = boost::locale::translate("HelloWorld").str(ko_loc2);

Finally, add the libboost_locale.dll.a library to the linker.

Character Set Conversions

std::string utf8_string = to_utf<char>(latin1_string,"EUC-KR");
std::wstring wide_string = to_utf<wchar_t>(latin1_string,"EUC-KR");
std::string latin1_string = from_utf(wide_string,"EUC-KR");
std::string utf8_string2 = utf_to_utf<char>(wide_string);

See also

Favorite site

References


  1. Boost.Locale-Messages_Formatting-Translation.pdf ↩

author Zer0 sitemap