C++ – boost to_upper function of string_algo doesn’t take into account the locale

boostcinternationalizationstring

I have a problem with the functions in the string_algo package.

Consider this piece of code:

#include <boost/algorithm/string.hpp>
int main() {
   try{
      string s = "meißen";
      locale l("de_DE.UTF-8");
      to_upper(s, l);
      cout << s << endl;
   catch(std::runtime_error& e){
      cerr << e.what() << endl;
   }

   try{
      string s = "composición";
      locale l("es_CO.UTF-8");
      to_upper(s, l);
      cout << s << endl;
   catch(std::runtime_error& e){
      cerr << e.what() << endl;
   }
}

The expected output for this code would be:

MEISSEN
COMPOSICIÓN

however the only thing I get is

MEIßEN
COMPOSICIóN

so, clearly the locale is not being taken into account. I even try to set the global locale with no success. What can I do?

Best Answer

In addition to the answer of Éric Malenfant -- std::locale facets works on single character. To get better result you may use std::wstring -- thus more characters would be converterd, but as you can see it is still not perfect (example ß).

I would suggest to give a try to Boost.Locale (new library for boost, not yet in boost), that does stuff

http://cppcms.sourceforge.net/boost_locale/docs/,

Especially see http://cppcms.sourceforge.net/boost_locale/docs/index.html#conversions that deals with the problem you are talking about.

Related Topic