locale in c++ n java
I have been sneaking into some code written by professionals (not to mention on the SO)..
I found people referencing often to the word locale
( for eg found some std::locale
). Even some code in java refers to locale
. So what is this locale
and why do we actually need it? And is it necessary to use it ( I guess it is used for some portability issues)? I tried googling but everything just ad开发者_开发百科ded to my confusion. :(
Locale is all about making your application look and feel right for users in multiple countries.
Simplistically, For example, in England we write 123456.78 as 123,456.78 but in France it is 123.456,78 using formatting and rendering functions that are locale aware will enable you application to look right in different countries.
It's used for internationalization and localization. For instance, if your program displays data on a web page, you might want to have versions of those web pages for different languages (French, English, Chinese, etc.). The locale facilities in languages like Java facilitate doing that.
See http://java.sun.com/javase/technologies/core/basic/intl/ for one overview.
"A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number should be formatted according to the customs/conventions of the user's native country, region, or culture."
(From JavaDocs: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html)
Most often you use it for times: CST, EST, PST, foreign countries, etc. Also for dates and different calendars, and different languages. Hence the representation of a geographical/political/cultural region.
Have you ever seen how many Europeans write numbers > 1000, many countries/languages use a period to separate the thousands while US and British English uses a comma. and the converse for decimal point. Locale has many uses, but one is to allow your program to format numbers and dates correctly by taking into account the user's language and, well, locale.
The easiest way to set the local in C++ is:
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 */
}
精彩评论