手头的任务
我正在从 Windows 上的 UTF-8 编码 XML 解析文件名。我需要将该文件名传递给我无法更改的函数。它在内部使用_fsopen()
不支持 Unicode 字符串的。
目前的方法
我目前的方法是将文件名转换为用户的字符集,希望文件名可以在该编码中表示。然后我boost::locale::conv::from_utf()
用来从 UTF-8 转换,我boost::locale::util::get_system_locale()
用来获取当前语言环境的名称。
生活很好?
我在使用代码页Windows-1252的德语系统上,因此get_system_locale()
正确生成de_DE.windows-1252。如果我使用包含变音符号的文件名测试该方法,那么一切都会按预期工作。
问题
只是为了确保我将系统区域设置切换为使用代码页Windows-1251的乌克兰语。在文件名中使用一些西里尔字母我的方法失败了。原因是get_system_locale()
仍然产生de_DE.windows-1252现在不正确。
另一方面GetACP()
,德国语言环境正确产生 1252,乌克兰语言环境正确产生 1251。我也知道 Boost.Locale 可以转换为给定的语言环境,因为这个小型测试程序可以按我的预期工作:
#include <boost/locale.hpp>
#include <iostream>
#include <string>
#include <windows.h>
int main()
{
std::cout << "Codepage: " << GetACP() << std::endl;
std::cout << "Boost.Locale: " << boost::locale::util::get_system_locale() << std::endl;
namespace blc = boost::locale::conv;
// Cyrillic small letter zhe -> \xe6 (ш on 1251, æ on 1252)
std::string const test1251 = blc::from_utf(std::string("\xd0\xb6"), "windows-1251");
std::cout << "1251: " << static_cast<int>(test1251.front()) << std::endl;
// Latin small letter sharp s -> \xdf (Я on 1251, ß on 1252)
auto const test1252 = blc::from_utf(std::string("\xc3\x9f"), "windows-1252");
std::cout << "1252: " << static_cast<int>(test1252.front()) << std::endl;
}
问题
如何以 Boost.Locale 支持的格式查询用户语言环境的名称?使用yield German_Germany.1252
std::locale("").name()
,使用它会导致异常。boost::locale::conv::invalid_charset_error
尽管我应该将其更改为本地管理员,但系统区域设置是否可能仍然是de_DE.windows-1252 ?同样,系统语言是德语,尽管我的帐户的语言是英语。(在我登录之前,登录屏幕是德语)
我应该坚持使用短文件名吗?虽然似乎不能可靠地工作。
印刷精美
- 编译器是 MSVC18
- Boost 是 1.56.0 版本,后端据说是 winapi
- 系统是Win7,系统语言是德语,用户语言是英语