How to write a wstring line contains different language to a file?

277 views Asked by At

I got seperated parts from 22 files in different languages and made them a wstring line like:

wstring wstr_line = L"\"IDS_TOAST_ECOON\",\"eco Mode is turned On.\",\"ecoモードをオンにしました。\",\"Režim eco je zapnutý.\",\"Økoindstillingen er aktiveret\"..."

I used wofstream to put wstr_line into a file, but the line finished at Japanese part(\"ecoモードをオンにしました。\"). If I set wfout.imbue("chs"); the line finished at Czech part(\"Režim eco je zapnutý.\")

How can write this line to a file correctly?

2

There are 2 answers

2
Martin York On

Try sticking this as the first line in your code:

int main()
{
    std::cout.imbue(std::locale(""));

This sets the local of the application to what the machine supports (which is probably UTF-32 for wide character strings). Unfortunately the default local is "C" for programers and the codecvt facet for the "C" local does not do anything useful (probably truncates wide charters to a single byte without conversion).

0
goss.beta On

I've solved the problem in another strategy, output the lines in bytes. Use the function below to output the wstring no matter what chracter it contains.

void output(ofstream &fout, vector<wstring> wline_list)
{
    void outputline(ofstream &, wstring);
  //pre output 0xFF and 0xFE to make the file encoding in UTF-16
    const BYTE PRE_LOW = 0xFF;
    const BYTE PRE_HIGH = 0xFE;
    fout << PRE_LOW << PRE_HIGH;
    for(vector<wstring>::size_type i(0); i<wline_list.size(); i++)
        outputline(fout, wline_list[i]);
}

void outputline(ofstream &fout, wstring line)
{
    void getByte(BYTE btchar[2], WORD wdChar);
    BYTE btChar[2] = {0,0};

    const BYTE CHANGE_LINE1_LOW = 0x0D;
    const BYTE CHANGE_LINE1_HIGH = 0x00;
    const BYTE CHANGE_LINE2_LOW = 0x0A;
    const BYTE CHANGE_LINE2_HIGH = 0x00;

    WORD wdChar(0);
    for(wstring::size_type i(0); i<line.length(); i++)
    {
        wdChar = line[i];
        getByte(btChar, wdChar);
        fout << btChar[0] << btChar[1];
    }
  //it needs this two chars to change line.
    fout << CHANGE_LINE1_LOW << CHANGE_LINE1_HIGH
        << CHANGE_LINE2_LOW << CHANGE_LINE2_HIGH;
}

void getByte(BYTE btchar[2], WORD wdChar)
{
    btchar[0] = wdChar % 0x0100;
    btchar[1] = wdChar / 0x0100;
}