C++ 中线程特定的区域设置操作
是否有任何标准方法可以跨平台每个线程进行区域设置?我看到 xlocale 提供了 uselocale,但在 Windows 中不支持。有“_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);”在 Windows 中,setlocale 在每个线程的基础上工作。我的问题是,是否有一个库可以以独立于平台的方式提供这些特定于区域设置的操作???或者还有其他方法吗?
谢谢, 戈库尔。
Is there any standard way doing locale setting across platforms per thread? I see that xlocale provides uselocale, but it is not supported in Windows. There is "_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);" in windows, after which setlocale works on per thread basis. My question is, is there a library that provides these locale specific manipulations in a platform independent way??? Or some other way of doing it?
Thanks,
Gokul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,最终我为其编写了一些小的交叉兼容性代码。我尝试尽可能遵循规范,但它确实有一些限制。
在此代码中 newlocale 不允许您指定基本区域设置,并且您也无法混合类别蒙版。但对于不同语言环境之间的一些基本切换来说,这应该足够了。
将该代码放入您的项目中,您将能够在任何平台上切换每个线程的区域设置,如下所示:
I've had the same problem and I eventually wrote some small cross-compatibility code for it. I tried to follow the specification as close as possible, but it does have a few limitations.
In this code newlocale does not allow you to specify a base locale and you're also not able to mix the category masks. But for some basic switching between different locales this should be enough.
Put that code in your project and you'll be able to switch the locale per-thread on any platform like this:
Boost::Locale 提供了一个跨平台的语言环境接口,基于 < a href="http://en.wikipedia.org/wiki/Iconv" rel="nofollow"> iconv 库。如果您希望 C 标准库函数在设置区域设置后立即正常运行,那么它可能不是您理想的界面。相反,您将手动将语言环境作为对象进行管理,并显式使用它们。您显式生成语言环境并使用它们创建比较器以及您需要的特定于区域设置的任何其他内容。
一方面,这意味着您之前编写的任何依赖于特定区域设置的代码都必须重写。另一方面,隐藏在引擎盖下的陷阱要少得多,因为您明确地处理了在哪里使用什么语言/编码。
Boost::Locale provides a locale interface which is cross-platform, based on the iconv library. It might not be your ideal interface if you're hoping for C standard library functions to behave appropriately as soon as you set the locale. You will, instead, manually manage locales as objects, and explicitly use them. You explicitly generate locales and use them to create comparators and whatever else you need that's locale specific.
On the one hand, this means any previously developed code you wrote which relies on locale being specific will have to be rewritten. On the other hand, there are many fewer gotchas hidden under the hood, as you're explicitly handling what language/encoding is used where.