localeについて

プログラムのlocaleについてよく知らなかったのでちょっとだけ調べてみた。


まず、localeとは何ですかというと、IT用語辞典によると、

ソフトウェアに内蔵される、言語や国・地域ごとに異なる単位、記号、日付、通貨などの表記規則の集合。または単に、利用する言語や国・地域の指定。多くのソフトウェアやプログラミング言語は、使用する言語とともにロケールを設定し、ロケールで定められた方式に基づいてデータの表記や処理を行う。


ということらしい。まあ、あれだ、単位とか日付の書き方とかをまとめたものね。で、プログラムは設定されたlocaleで出力しますよと。


で、通常はプログラム起動時にsetlocale(3)を使ってlocaleを設定する。setlocale(3)を見てみるとこんな感じ。

# man setlocale
SETLOCALE(3)               Linux Programmer's Manual              SETLOCALE(3)

NAME
       setlocale - set the current locale

SYNOPSIS
       #include <locale.h>

       char *setlocale(int category, const char *locale);

DESCRIPTION
       The  setlocale() function is used to set or query the program's current
       locale.

       If locale is not NULL, the program's current locale is modified accord‐
       ing  to the arguments.  The argument category determines which parts of
       the program's current locale should be modified.


categoryでlocaleを設定する対象を指定して、localeで設定するlocaleを指定する。categoryで設定できるのは、次の7つ。

LC_ALL for all of the locale.
LC_COLLATE
for regular expression matching (it determines the meaning of
range expressions and equivalence classes) and string collation.
LC_CTYPE
for regular expression matching, character classification, con‐
version, case-sensitive comparison, and wide character func‐
tions.
LC_MESSAGES
for localizable natural-language messages.
LC_MONETARY
for monetary formatting.
LC_NUMERIC
for number formatting (such as the decimal point and the thou‐
sands separator).
LC_TIME
for time and date formatting.

だいたいはLC_ALLを使うみたいね。
次はlocale。


こちらは、正しいlocale値を設定すればそのlocaleに設定される。NULLの場合は、現在のlocaleが返る。""を設定すると、環境変数からlocaleを設定してくれるということらしい。