Boost logo

Boost Users :

Subject: [Boost-users] Question about boost::locale translations
From: Bastien Montagne (montagne29_at_[hidden])
Date: 2012-11-05 13:19:39


Hi everybody! :)

I’m currently trying to replace libintl (from gnu gettext) by
boost::locale lib, in a (mostly) C program (blender, actually).

To do so, I wrote a simplistic wrapper:

#include <boost/locale.hpp>

#include "boost_locale_wrapper.h"

static boost::locale::generator gen;

/* Called only once */
void boost_locale_init(const char *messages_path, const char
*default_domain)
{
     gen.add_messages_path(messages_path);
     gen.add_messages_domain(default_domain);
     gen.set_default_messages_domain(default_domain);
}

/* Called each time you change locale */
void boost_locale_set(const char *locale)
{
     if (locale && locale[0]) {
         std::locale::global(gen(locale));
     }
     else {
         std::locale::global(gen(""));
     }
}

const char* boost_locale_gettext(const char *msgid)
{
     return boost::locale::gettext(msgid).c_str();
}

(this wrapper lib is linked statically with the main app)

But I'm facing a big problem: I would expect strings returned by
boost::locale::gettext & co to be "const", i.e. that the char pointer
gotten by boost::locale::gettext(msgid).c_str() would be const (and
unique to a given locale/msgid/context/etc.), as it is with gnu gettext.
But this is obviously not the case, as demonstrated by those prints
(translated_msgid (address)):

Serbe latin (Srpski latinica) (0x7fcaed0dac58)
sr_RS_at_latin (0x7fcaee1e5628)
Suédois (Svenska) (0x7fcaed0dac58)
sv_SE (0x7fcaee1e5628)

Am I missing something obvious here, or are returned string just not
"const"? If so, how can a code like:

MessageBoxW(0,pgettext
<http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/group__message.html#ga2a39910bdf57e4d151607ee1a115b853>(L"File
Dialog",L"Open?").c_str(),gettext
<http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/group__message.html#gadd3006a6d5d1c804490152ee6300d47f>(L"Question").c_str(),MB_YESNO);

(taken from boost doc) could work?

I'm quite new to all this C++2C stuff (and to boost too), so please
forgive me if this is a noob question...

Best regards,
Bastien


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net