You can use message_format facet directly

   http://www.boost.org/doc/libs/1_52_0/libs/locale/doc/html/group__message.html#ga4f65e4e1c3995eb09dd8f8f0e150a012

i.e.

char const *gettext(char const *input)
{
   try {
     std::locale l;
     boost::locale::message_facet<char> const &facet = std::use_facet<boost::locale::message_facet<char> >(l);
     char const *r = facet.get(my_domain_id,0,input);
     if(r) return r;
     return input;
   }
   catch(std::exception const &e) {
     return input;
   }
}

Note the input must have the SAME encoding as the locale.
 
Artyom Beilis
--------------
CppCMS - C++ Web Framework:   http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/


From: Bastien Montagne <montagne29@wanadoo.fr>
To: boost-users@lists.boost.org
Sent: Tuesday, November 6, 2012 11:42 AM
Subject: Re: [Boost-users] Question about boost::locale translations

I suspected something like that (and indeed it worked with a strdup). Unfortunately, I’m not sure there will be a nice and suitable way to handle this, will try to find something…

Anyway, thank you very much for the answer! :)

On 06/11/2012 10:28, Dominique Devienne wrote:
On Mon, Nov 5, 2012 at 7:19 PM, Bastien Montagne <montagne29@wanadoo.fr> wrote:
const char* boost_locale_gettext(const char *msgid)
{
    return boost::locale::gettext(msgid).c_str();
}

The line above decomposes so:

1) std::string hidden_tmp1 = boost::locale::gettext(msgid)
2) const char* hidden_tmp2 = hidden_tmp1.c_str() // get pointer to internal buffer
3) ; // delete hidden_tmp1, which frees buffer hidden_tmp2 points to. Temporaries live until the semi-colon.
4) return hidden_tmp2 // pointer to freed (stale) memory. Not good.
 
I'm quite new to all this C++2C stuff (and to boost too), so please forgive me if this is a noob question...

You need to strdup() the c_str(), return a pointer to that, *and* document that callers are responsible for freeing the memory  returned to them. Or your wrapper keeps track of this memory itself somehow, and releases down the line somehow again, so the callers do not need to explicitly free those strings. --DD



_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users