【问题标题】:locale in c++ n javac++ n java中的语言环境
【发布时间】:2011-01-16 18:18:15
【问题描述】:
我一直在潜入一些由专业人士编写的代码(更不用说 SO)..
我发现人们经常提到locale这个词(例如发现一些std::locale)。甚至 java 中的一些代码也引用了 locale 。那么这个locale 是什么,为什么我们真的需要它呢?是否有必要使用它(我猜它用于一些可移植性问题)?我试着用谷歌搜索,但一切都让我更加困惑。 :(
【问题讨论】:
标签:
java
c++
localization
internationalization
【解决方案1】:
区域设置旨在让您的应用程序在外观和感觉上都适合多个国家/地区的用户。
简单地说,例如,在英格兰,我们将 123456.78 写为 123,456.78,但在法国,它是 123.456,78,使用可识别区域设置的格式化和渲染功能将使您的应用程序在不同国家/地区看起来正确。
【解决方案4】:
您是否见过有多少欧洲人写出大于 1000 的数字,许多国家/语言使用句点来分隔千位,而美式英语和英式英语使用逗号。小数点反之。语言环境有很多用途,但其中之一是允许您的程序通过考虑用户的语言以及语言环境来正确格式化数字和日期。
【解决方案5】:
在 C++ 中设置本地的最简单方法是:
int main()
{
// If the string is empty (as here)
// Then it looks at the machine current configuration and retrieves
// the local that you have set up in the configuration of the machine
// This is what you normally want to happen when your customers run the code.
std::locale::global(std::locale(""));
// Setting the locale affects a whole host of things that happen with streams.
/* YOUR CODE */
}