Skip to content

Boost:Locale

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

우선 locale/en/LC_MESSAGES/messages.po파일과 locale/ko_KR/LC_MESSAGES/messages.po파일을 작성한다. 아래의 코드는 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 "안녕, 세상!"

(참고로 en의 경우 msgstr "안녕, 세상!"대신 msgstr "Hello, World!"를 입력했다.)

아래와 같이 테스트할 수 있다.

#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);

마지막으로 링커에 libboost_locale.dll.a라이브러리를 추가하면 된다.

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