std::setlocale
| ヘッダー <clocale> で定義 |
||
| char* setlocale( int category, const char* locale ); |
||
setlocale 関数は、指定されたシステムのロケールまたはその一部を新しいCロケールとしてインストールします。この変更は、次に setlocale が呼び出されるまで有効であり、ロケールに依存するすべてのCライブラリ関数の実行に影響を与えます。locale がヌルポインタの場合、setlocale は現在のCロケールを変更せずに照会します。
目次 |
[編集] パラメータ
| category | - | ロケールカテゴリ識別子。LC_xxx マクロのいずれか。0 である場合もあります。 |
| locale | - | システム固有のロケール識別子。ユーザー推奨のロケールには ""、最小限のロケールには "C" を指定できます。 |
[編集] 戻り値
変更が適用された後のCロケールを識別する、ヌル終端の窄文字列へのポインタ。失敗した場合はヌルポインタ。
返された文字列のコピーは、この std::setlocale 呼び出しで使用されたカテゴリとともに、プログラムの後半でロケールをこの呼び出しの終了時の状態に戻すために使用できます。
[編集] 注記
プログラムの起動時、ユーザーコードが実行される前に、std::setlocale(LC_ALL, "C"); と同等の処理が実行されます。
戻り値の型は char* ですが、ポインタが指す文字を変更することは未定義の動作です。
setlocale はグローバル状態を変更し、ロケール依存の関数の実行に影響を与えるため、あるスレッドから setlocale を呼び出し、同時に別のスレッドが以下のいずれかの関数を実行している場合、未定義の動作となります: std::fprintf, std::isprint, std::iswdigit, std::localeconv, std::tolower, std::fscanf, std::ispunct, std::iswgraph, std::mblen, std::toupper, std::isalnum, std::isspace, std::iswlower, std::mbstowcs, std::towlower, std::isalpha, std::isupper, std::iswprint, std::mbtowc, std::towupper, std::isblank, std::iswalnum, std::iswpunct, std::setlocale, std::wcscoll, std::iscntrl, std::iswalpha, std::iswspace, std::strcoll, std::wcstod, std::isdigit, std::iswblank, std::iswupper, std::strerror, std::wcstombs, std::isgraph, std::iswcntrl, std::iswxdigit, std::strtod, std::wcsxfrm, std::islower, std::iswctype, std::isxdigit。
POSIX は、常にアクセス可能で、デフォルトの最小限の "C" ロケールと完全に等価な "POSIX" という名前のロケールも定義しています。
POSIX はまた、返されるポインタが、ポインタが指す文字列の内容だけでなく、後続の setlocale 呼び出しによって無効になる可能性があることを指定しています。
[編集] 例
#include <clocale> #include <cstdio> #include <ctime> #include <cwchar> #include <iterator> #include <string> int main() { // Make a "deep copy" of current locale name. std::string prev_loc = std::setlocale(LC_ALL, nullptr); // The C locale will be UTF-8 enabled English, // decimal dot will be German, // date and time formatting will be Japanese. if (const char* loc = std::setlocale(LC_ALL, "en_US.UTF-8")) std::wprintf(L"New LC_ALL locale: %s\n", loc); if (const char* loc = std::setlocale(LC_NUMERIC, "de_DE.UTF-8")) std::wprintf(L"New LC_NUMERIC locale: %s\n", loc); if (const char* loc = std::setlocale(LC_TIME, "ja_JP.UTF-8")) std::wprintf(L"New LC_TIME locale: %s\n", loc); wchar_t buf[100]; std::time_t t = std::time(nullptr); std::wcsftime(buf, std::size(buf), L"%A %c", std::localtime(&t)); std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, buf); // Restore the previous locale. if (const char* loc = std::setlocale(LC_ALL, prev_loc.c_str())) std::wprintf(L"Restorred LC_ALL locale: %s\n", loc); }
実行結果の例
New LC_ALL locale: en_US.UTF-8 New LC_NUMERIC locale: de_DE.UTF-8 New LC_TIME locale: ja_JP.UTF-8 Number: 3,14 Date: 日曜日 2022年11月06日 20時40分59秒 Restorred LC_ALL locale: C
[編集] 関連項目
| std::setlocale のロケールカテゴリ (マクロ定数) | |
| 文化的な違いをカプセル化するポリモーフィックなファセットのセット (クラス) | |
| C ドキュメント: setlocale
| |
[編集] 外部リンク
| 1. | Windows のロケール名一覧. |
| 2. | Linux のロケール名一覧. |