|
Boost Users : |
Subject: Re: [Boost-users] Question about boost::locale translations
From: Artyom Beilis (artyomtnk_at_[hidden])
Date: 2012-11-06 05:11:38
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_at_[hidden]>
>To: boost-users_at_[hidden]
>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_at_[hidden]> 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_at_[hidden] http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>_______________________________________________
>Boost-users mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
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