如何将包含不同语言的wstring行写入文件

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

本文关键字:wstring 文件 语言 包含不      更新时间:2023-10-16

我从22个不同语言的文件中获得了单独的部分,并将它们制作成一个wstring行,如:

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

我使用wofstream将wstr_line放入一个文件中,但这一行在日语部分结束(\"ecoモードをオンにしました。\")。如果我设置wfout.imbue("chs");,则线路在捷克部分完成(\"Režim eco je zapnutý\")

如何将这一行正确地写入文件?

试着把它作为代码中的第一行:

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

这将应用程序的本地设置为机器支持的内容(对于宽字符串,可能是UTF-32)。不幸的是,对于程序员来说,默认的local是"C",而"C"local的codecvt方面没有做任何有用的事情(可能会在没有转换的情况下将宽宪章截断为单个字节)。

我已经用另一种策略output the lines in bytes解决了这个问题。使用下面的函数来输出wstring,无论它包含什么chracter。

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;
}