Click here to Skip to main content
15,891,888 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used to code for Windows now I'm trying to make the code work for Linux. This is a sample code that I'd like to ask a question about:

#include <iostream>

int main (int argc, char * argv []) {
setlocale (LC_ALL, "be_BY.utf8");
const char * HelloWorld_char = "Вітаю, шаноўны спадар!";
const wchar_t * HelloWorld_wchar_t = L"Вітаю, шаноўны спадар!";
std::cout << HelloWorld_char << std::endl;
std::wcout << HelloWorld_wchar_t << std::endl;
return 0;
}

The source file is UTF-8 encoded. I compile it with

g++ -fexec-charset=utf-8 -fwide-exec-charset=utf-8 test.cpp

The line of code with cout works (no matter what locale I set with setlocale !?). But wcout prints rubbish. Can you explain why? Is it possible to localize wcout as well? Thanks

PS. Well, the strings don't look nice on my page but they just contain "Hello, World!" text in Belarusian.
Posted
Updated 23-Aug-10 10:52am
v2

There are two distinct things that should cooperate.

1. The way the source code is written: make shure your editor saved the CPP files with UTF-8 encoding and not as an SBCS or MBCS codepage, otherwise the compiler sees the literal strings differently on what you expect.

2. cout and wcout use the C++ global std:locale facets to perform their task. They are not necessarily synchronized with the C locales.
The proper way is instantiate a C++ local class, and imbue it in the streams, like

C++
#include <locale>
#include <iostreams>
int main()
{
    std::locale loc("be_BY.utf8");
    std::cout.imbue(loc);
    std::wcout.imbue(loc);

    //Your writings goes here, remove the setlocale call
}


This[^] may also be helpful.
 
Share this answer
 
v2
Comments
japcrword 24-Aug-10 16:20pm    
Thank you very much for the advice and for the article (it is certainly useful for me as I'm not an expert in Unicode). I followed your advice. The output was:

terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted

From which I concluded that the needed locale wasn't installed. For the list of available locales I ran 'locale -a' (just to make sure the be_BY.utf8 locale wasn't present - and it wasn't). I have installed the locale with 'locale-gen be_BY.utf8'. Now the program doesn't throw an exception but still wcout doesn't work. The results of calling setlocale and wcout.imbue are (as far as I can judge based on output) identical. I did check source file encoding. It's utf8. I've verified my code under MS Windows 7 + MS Visual C 2008 (though I used different locale string). Both cout and wcout worked properly. But under Linux / G++ the wcout behaviour differs. I'm using Ubuntu 10.04 Lucid Lynx 64 bit and g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3.
My problem is the same, I use Fedora 13 x86_64. Is it a bug or when we used "setlocale" we modified something in our configs?
 
Share this answer
 
Comments
japcrword 9-Oct-10 18:06pm    
Well, currently I don't know. I prefer to think that it is not a bug. I simply decided to use cout + utf8 instead of wcout on linux.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900