Locale.h

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

locale.hC程式設計語言標準函式庫的一個標頭檔,聲明了C語言在地化函式[1][2] 這些函式用於在處理多種自然語言的軟體編程設計時,把程式調整到特定的區域設定. 這些區域設定影響到C語言標準庫的輸入/輸出函式.

所包含的函式[編輯]

C語言在地化函式與資料類型定義在locale.h (clocale標頭檔用於C++).[3]

Function Description
setlocale 設定與讀取當前C locale
localeconv 返回當前locale的貨幣與數值的格式細節

批評[編輯]

C語言標準中定義的區域設定函式的最大問題是,區域設定狀態是全域的。這意味著一個程式在一個時刻只能有一個locale設定。實現程式同時具有多個locale是非常困難的。[4]

Visual C++執行時刻庫定義了函式_configthreadlocale[5],可以打開或者關閉執行緒相關區域設定(thread-specific locale)。另外一個執行時刻庫函式_setmbcp,線上程中建立限於本執行緒使用的區域設定資料結構。此後,就可以在該執行緒使用setlocale標準庫函式了。但此種方法僅限於Windows平台。

例子[編輯]

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main(void)
{
    /* Locale is set to "C" before this. This call sets it
       to the "current locale" by reading environment variables: */
    setlocale(LC_ALL, "");

    const struct lconv * const currentlocale = localeconv();

    printf("In the current locale, the default currency symbol is: %s\n",
        currentlocale->currency_symbol);

    return EXIT_SUCCESS;
}

參考文獻[編輯]

  1. ^ ISO/IEC 9899:1999 specification (PDF). . p. 204, § 7.11 Localization [2012-09-02]. (原始內容存檔 (PDF)於2011-01-24). 
  2. ^ Prata, Stephen. C primer plus. Sams Publishing. 2004. Appendix B, Section V: The Standard ANSI C Library with C99 Additions. ISBN 0-672-32696-5. 
  3. ^ locale.h. utas.edu.au. infosys. [14 September 2011]. (原始內容存檔於2012-06-04). 
  4. ^ The Standard C Locale and the Standard C++ Locales. Rogue Wave Software, Inc. 1996 [2012-09-02]. (原始內容存檔於2020-02-19). 
  5. ^ Microsoft的MSDN上的相关帮助文章. [2012-09-06]. (原始內容存檔於2016-05-19).