[Boost-users] Boost 1.49 Locale Conversion failed

35 views
Skip to first unread message

NtVisigoth

unread,
Jul 10, 2012, 5:36:32 AM7/10/12
to boost...@lists.boost.org
Hello, everyone!

But previous latter badly typed.

When using boost::locale with the russian language of the string in UTF-8 encoding I get an error while getting the locale.
An exception with the message e.what(): "Conversion failed" is thrown

My code is not very different from that in the examples:

#define  TR(S)  boost::locale::translate(S).str()
const std::string strDefaultLangId( "en" );
int main( int argc, char * argv[] )
{
    using namespace std;
    using namespace boost::locale;
    int exitCode = EXIT_SUCCESS;
    try
    {
        generator gen;
        gen.add_messages_path("g:\\source\\boost_locale\\languages\\");
        gen.add_messages_domain("messages");
        std::string language( argc == 2 ? argv[1] : strDefaultLangId.c_str() );
        std::locale loc = gen( language );
        locale::global( loc );
        std::wstring s = TR(L"hello world");
        wcout << s << endl;
    }
    catch( exception& e )
    {
        cout << "Error : " << e.what() << endl;
        exitCode = EXIT_FAILURE;
    }
    catch( ... )
    {
        cout << "Unknown error" << endl;
        exitCode = EXIT_FAILURE;
    }
    return exitCode;
}


The path to the file with the russian language: "g:\source\boost_locale\languages\ru\LC_MESSAGES\messages.mo"

What could be wrong?
* Is it necessary to use the backends?
* Is it yet necessary to call imbue on the stream? But I'd like avoid it because I am going to write the log using glog. 

Compillers : Microsoft Visual Studio 2008 and 2010
Boost version : 1.49

Artyom Beilis

unread,
Jul 10, 2012, 6:27:44 AM7/10/12
to boost...@lists.boost.org
The locale name should be "en_US.UTF-8" or "ru_RU.UTF-8", the default UTF-8 locale is for
default system locale "".

When you set locale as "en" it assumes that encoding (as not specified) is US-ASCII and conversion fails.
 
Artyom Beilis
--------------
CppCMS - C++ Web Framework:   http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/


From: NtVisigoth <ntvis...@gmail.com>
To: boost...@lists.boost.org
Sent: Tuesday, July 10, 2012 12:36 PM
Subject: [Boost-users] Boost 1.49 Locale Conversion failed
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

NtVisigoth

unread,
Jul 10, 2012, 9:16:28 AM7/10/12
to boost...@lists.boost.org
2012/7/10 Artyom Beilis <arty...@yahoo.com>

I have :
1) Russian language Filename : g:\project\proba\locale\ru_RU.UTF-8\LC_MESSAGES\messages.mo
2) Russian language locale name in С++-source:
const  std::string  ruLanguage( "ru_RU" )

Is it correct?

I dont understand How Boost Locale finds languages MO-file by their locale names in C++-source.

Artyom Beilis

unread,
Jul 10, 2012, 9:27:11 AM7/10/12
to boost...@lists.boost.org
>________________________________
> From: NtVisigoth <ntvis...@gmail.com>
>To: boost...@lists.boost.org
>Sent: Tuesday, July 10, 2012 4:16 PM
>Subject: Re: [Boost-users] Boost 1.49 Locale Conversion failed>I have :

>
>1) Russian language Filename : g:\project\proba\locale\ru_RU.UTF-8\LC_MESSAGES\messages.mo
>2) Russian language locale name in С++-source:
>const  std::string  ruLanguage( "ru_RU" )
>
>
>Is it correct?

No

   Filename : g:\project\proba\locale\ru\LC_MESSAGES\messages.mo
   locale name in С++-source:"ru_RU.UTF-8"


>
>
>I dont understand How Boost Locale finds languages MO-file by their locale names in C++-source.


See: http://www.boost.org/doc/libs/1_50_0/libs/locale/doc/html/messages_formatting.html
 

Artyom Beilis
--------------
CppCMS - C++ Web Framework:   http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/

NtVisigoth

unread,
Jul 10, 2012, 10:17:30 AM7/10/12
to boost...@lists.boost.org
2012/7/10 Artyom Beilis <arty...@yahoo.com>

I written another proba-project:

const  std::string  strLanguagesPath( "g:\\source\\boost_locale\\languages\\" );
const  std::string  strLanguageFilename( "messages" );
const  std::string  strEnLanguage( "en_US.UTF-8" );
const  std::string  strRuLanguage( "ru_RU.UTF-8" );

int main( int argc, char * argv[] )
{
    using namespace std;
    using namespace boost::locale;

    int exitCode = EXIT_SUCCESS;
    try
    {
        generator gen;

        gen.add_messages_path( strLanguagesPath );
        gen.add_messages_domain( strLanguageFilename );

        cout << "Start Boost.Locale proba" << endl;
        std::locale en_loc = gen( strEnLanguage );
        wcout << TREX( L"hello world", en_loc ) << endl;

        std::locale ru_loc = gen( strRuLanguage );
        wcout << TREX( L"hello world", ru_loc ) << endl;
        cout << "Stop Boost.Locale proba" << endl;

File paths:
g:\source\boost_locale\languages\en\LC_MESSAGES\messages.mo
g:\source\boost_locale\languages\ru\LC_MESSAGES\messages.mo

and I see in STDOUT following:
Start Boost.Locale proba
Hello world Localized
Stop Boost.Locale proba
Press any key to continue . . .

There no any russian strings in STDOUT. Why?

Viatchesla...@h-d-gmbh.de

unread,
Jul 11, 2012, 7:41:02 AM7/11/12
to boost...@lists.boost.org
On Tue, 10 Jul 2012 16:17:30 +0200, NtVisigoth <ntvis...@gmail.com>
wrote:
>
> I written another proba-project:
>
> const std::string strLanguagesPath(
> "g:\\source\\boost_locale\\languages\\" );
> const std::string strLanguageFilename( "messages" );
> const std::string strEnLanguage( "en_US.UTF-8" );
> const std::string strRuLanguage( "ru_RU.UTF-8" );
>
> int main( int argc, char * argv[] )
> {
> using namespace std;
> using namespace boost::locale;
>
> int exitCode = EXIT_SUCCESS;
> try
> {
> generator gen;
>
> gen.add_messages_path( strLanguagesPath );
> gen.add_messages_domain( strLanguageFilename );
>
> cout << "Start Boost.Locale proba" << endl;
> std::locale en_loc = gen( strEnLanguage );
> wcout << TREX( L"hello world", en_loc ) << endl;
>
> std::locale ru_loc = gen( strRuLanguage );
> wcout << TREX( L"hello world", ru_loc ) << endl;
> cout << "Stop Boost.Locale proba" << endl;
>

Hi,

I have very little experience with locales, but would note few things here:

1) You're using wcout for UTF-8 - that's not legal, at least in gcc world
(have no dev win machine under hand)

2) what's TREX macro? Please always provide full test case.

3) May it be that your console cannot output non-ASCII symbols? Look at
the string in debugger just to be sure.

-- Slava

NtVisigoth

unread,
Jul 11, 2012, 7:45:15 AM7/11/12
to boost...@lists.boost.org

2) I am sorry. I use next macroses:
#define  TR(S)      boost::locale::translate(S)
#define  TREX(S,L)  boost::locale::translate(S).str(L)

3) You are right! See:
c:\>chcp
Active code page: 866

Vicente J. Botet Escriba

unread,
Jul 12, 2012, 7:34:39 AM7/12/12
to boost...@lists.boost.org
Le 10/07/12 16:17, NtVisigoth a écrit :
Hi,

I guess you need to associate the locale ru_loc on the stream wcout.


Vicente
Reply all
Reply to author
Forward
0 new messages