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