What is setlocale() for?

做~自己de王妃 提交于 2020-07-22 18:16:31

问题


I'm studying c++ and I have found this function of C++ library: setlocale (http://www.cplusplus.com/reference/clocale/setlocale/) but I'm not able to understand what is it for.

I have used on Ubuntu:

printf ("Locale is: %s\n", setlocale(LC_ALL,"") );

it prints Locale is: en_US.UTF-8

but on macOs it prints:

Locale is: C what does means this C?

In what context and how should it be used?


回答1:


Read on Linux the setlocale(3) and locale(7) man pages. Read also the internationalization and localization wikipage.

On Debian and Ubuntu, you might run (as root) the dpkg-reconfigure locales to add more locales.

Then you could set your LANG (and LC_ALL and others) environment variables (see environ(7)) to change the language of message.

For example, I have the French UTF-8 locale installed. If I do

% env LC_ALL=fr_FR.UTF-8 ls /tmp/nonexisting 

I'm getting the error message in French:

ls: impossible d'accéder à '/tmp/nonexisting': Aucun fichier ou dossier de ce type

If I use the C locale (which is the default one), it is in English:

% env LC_ALL=C ls /tmp/nonexisting
ls: cannot access '/tmp/nonexisting': No such file or directory

As a rule of thumb, you want to export LC_ALL=C LANG=C before running commands that you exhibit on this forum (e.g. because you don't want to show error messages from the compiler or the shell in French).

If you are coding a program which you want to internationalize (ez.g. to be easily usable by people understanding only French or Chinese), you need at least to use gettext(3) (notably for printf format strings!) and perhaps textdomain(3) and you'll need to use msgfmt(1) for dealing with message catalogs. Of course you'll need to build catalogs of translated messages.

Localization also influences how numbers are parsed and printed (with a comma or a dot separating the thousands or the decimal digits) and how money and time get printed and parsed (e.g. strftime(3)).




回答2:


It's locale specific settings. For example, in some countries comma is used for decimal separator, in others, it's dot. In the US 22,001 is often understood to be 22 thousand and 1, in some European countries that's 22 point 001.

Dates can be given in DD/MM/YYYYY (most of Europe) or MM/DD/YYYY (in the US) format and so forth.



来源:https://stackoverflow.com/questions/46939891/what-is-setlocale-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!