setlocale()的作用是什么? [英] What is setlocale() for?

查看:97
本文介绍了setlocale()的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究c ++,并且发现了C ++库的以下功能: setlocale ( http://www.cplusplus.com/reference/clocale/setlocale/),但我无法理解它的用途.

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.

我在Ubuntu上使用过:

I have used on Ubuntu:

printf(语言环境为:%s \ n",setlocale(LC_ALL,"));

它打印语言环境为:en_US.UTF-8

但是在macOS上它会打印:

but on macOs it prints:

语言环境为:C 这是什么意思C?

在什么情况下以及如何使用?

In what context and how should it be used?

推荐答案

在Linux上阅读 setlocale(3) locale(7)手册页.另请阅读国际化和本地化维基页面.

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

在Debian和Ubuntu上,您可以(以root用户身份)运行 dpkg-reconfigure语言环境以添加更多语言环境.

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

然后,您可以设置 LANG (以及 LC_ALL 等)环境变量(请参见

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

例如,我安装了法语UTF-8语言环境.如果我这样做

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

如果我使用 C 语言环境(这是默认语言环境),它是英文的:

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

根据经验,您希望在运行在此论坛上展示的命令之前,先导出LC_ALL = C LANG = C (例如,因为您不想显示来自编译器的错误消息)或法语中的外壳).

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).

如果您要编码一个要国际化的程序(例如,易于理解法语或中文的人可以轻松使用),则至少需要使用 textdomain(3),您会需要使用 msgfmt(1)处理消息目录.当然,您需要构建翻译后的邮件目录.

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)).

这篇关于setlocale()的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆