CultureInfo.CreateSpecificCulture(String) 方法

定義

建立 CultureInfo,代表與指定名稱相關的特定文化特性。

C#
public static System.Globalization.CultureInfo CreateSpecificCulture(string name);

參數

name
String

預先定義的 CultureInfo 名稱或現有 CultureInfo 物件的名稱。 name 不區分大小寫。

傳回

CultureInfo 物件,代表:

如果 name 是空字串 (""),則為不因文化特性而異 (Invariant Culture)。

-或-

如果 name 是中性文化特性,則為與 name 關聯的特定文化特性。

-或-

如果 name 已經是特定文化特性,則為 name 所指定的文化特性。

例外狀況

name 不是有效的文化特性名稱。

-或-

name 所指定的文化特性沒有相關聯的特定文化特性。

name 為 null。

範例

下列範例會從 GetCultures 方法擷取代表中性文化特性的物件CultureInfo陣列,並排序陣列。 當反覆運算陣列中的專案時,它會將每個中性文化 CreateSpecificCulture 特性的名稱傳遞給方法,並顯示方法所傳回的特定文化特性名稱。

備註

此範例會使用 zh-CHSzh-CHT 文化特性名稱。 不過,以 Windows Vista 和更新版本為目標的應用程式應該使用 zh-Hans 而不是 zh-CHSzh-Hant ,而不是 zh-CHT。 zh-Hanszh-Hant 代表目前的標準,除非您有使用舊名稱的原因,否則應該使用 。

另請注意,範例的結果在安裝臺灣 Windows 時可能會有所不同,其中中文 (傳統) 中性文化特性的輸入 (zh、zh-CHT 或 zh-Hant) 將會傳回 zh-TW。

C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Display the header.
      Console.WriteLine("{0,-53}{1}", "CULTURE", "SPECIFIC CULTURE");

      // Get each neutral culture in the .NET Framework.
      CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
      // Sort the returned array by name.
      Array.Sort<CultureInfo>(cultures, new NamePropertyComparer<CultureInfo>());

      // Determine the specific culture associated with each neutral culture.
      foreach (var culture in cultures)
      {
         Console.Write("{0,-12} {1,-40}", culture.Name, culture.EnglishName);
         try {
            Console.WriteLine("{0}", CultureInfo.CreateSpecificCulture(culture.Name).Name);
         }
         catch (ArgumentException) {
            Console.WriteLine("(no associated specific culture)");
         }
      }
   }
}

public class NamePropertyComparer<T> : IComparer<T>
{
   public int Compare(T x, T y)
   {
      if (x == null)
         if (y == null)
            return 0;
         else
            return -1;

      PropertyInfo pX = x.GetType().GetProperty("Name");
      PropertyInfo pY = y.GetType().GetProperty("Name");
      return String.Compare((string) pX.GetValue(x, null), (string) pY.GetValue(y, null));
   }
}
// The example displays the following output on a Windows system.  This output has been cropped for brevity.
//    CULTURE                                              SPECIFIC CULTURE
//                 Invariant Language (Invariant Country)
//    aa           Afar                                    aa-ET
//    af           Afrikaans                               af-ZA
//    agq          Aghem                                   agq-CM
//    ak           Akan                                    ak-GH
//    am           Amharic                                 am-ET
//    ar           Arabic                                  ar-SA
//    arn          Mapudungun                              arn-CL
//    as           Assamese                                as-IN
//    asa          Asu                                     asa-TZ
//    ast          Asturian                                ast-ES
//    az           Azerbaijani                             az-Latn-AZ
//    az-Cyrl      Azerbaijani (Cyrillic)                  az-Cyrl-AZ
//    az-Latn      Azerbaijani (Latin)                     az-Latn-AZ
//    ba           Bashkir                                 ba-RU
//    bas          Basaa                                   bas-CM
//    be           Belarusian                              be-BY
//    bem          Bemba                                   bem-ZM
//    bez          Bena                                    bez-TZ
//    bg           Bulgarian                               bg-BG
//    bin          Edo                                     bin-NG
//    bm           Bamanankan                              bm-Latn-ML
//    bm-Latn      Bamanankan (Latin)                      bm-Latn-ML
//    bn           Bangla                                  bn-BD
//    bo           Tibetan                                 bo-CN
//    br           Breton                                  br-FR
//    brx          Bodo                                    brx-IN
//    bs           Bosnian                                 bs-Latn-BA
//    bs-Cyrl      Bosnian (Cyrillic)                      bs-Cyrl-BA
//    bs-Latn      Bosnian (Latin)                         bs-Latn-BA
//    byn          Blin                                    byn-ER
//    ca           Catalan                                 ca-ES
//    ce           Chechen                                 ce-RU
//    cgg          Chiga                                   cgg-UG
//    chr          Cherokee                                chr-Cher-US
//    chr-Cher     Cherokee                                chr-Cher-US
//    co           Corsican                                co-FR
//    cs           Czech                                   cs-CZ
//    cu           Church Slavic                           cu-RU
//    cy           Welsh                                   cy-GB
//    da           Danish                                  da-DK
//    dav          Taita                                   dav-KE
//    de           German                                  de-DE
//    dje          Zarma                                   dje-NE
//    dsb          Lower Sorbian                           dsb-DE
//    dua          Duala                                   dua-CM
//    dv           Divehi                                  dv-MV
//    ...
//    ta           Tamil                                   ta-IN
//    te           Telugu                                  te-IN
//    teo          Teso                                    teo-UG
//    tg           Tajik                                   tg-Cyrl-TJ
//    tg-Cyrl      Tajik (Cyrillic)                        tg-Cyrl-TJ
//    th           Thai                                    th-TH
//    ti           Tigrinya                                ti-ER
//    tig          Tigre                                   tig-ER
//    tk           Turkmen                                 tk-TM
//    tn           Setswana                                tn-ZA
//    to           Tongan                                  to-TO
//    tr           Turkish                                 tr-TR
//    ts           Tsonga                                  ts-ZA
//    tt           Tatar                                   tt-RU
//    twq          Tasawaq                                 twq-NE
//    tzm          Central Atlas Tamazight                 tzm-Latn-DZ
//    tzm-Arab     Central Atlas Tamazight (Arabic)        tzm-Arab-MA
//    tzm-Latn     Central Atlas Tamazight (Latin)         tzm-Latn-DZ
//    tzm-Tfng     Central Atlas Tamazight (Tifinagh)      tzm-Tfng-MA
//    ug           Uyghur                                  ug-CN
//    uk           Ukrainian                               uk-UA
//    ur           Urdu                                    ur-PK
//    uz           Uzbek                                   uz-Latn-UZ
//    uz-Arab      Uzbek (Perso-Arabic)                    uz-Arab-AF
//    uz-Cyrl      Uzbek (Cyrillic)                        uz-Cyrl-UZ
//    uz-Latn      Uzbek (Latin)                           uz-Latn-UZ
//    vai          Vai                                     vai-Vaii-LR
//    vai-Latn     Vai (Latin)                             vai-Latn-LR
//    vai-Vaii     Vai (Vai)                               vai-Vaii-LR
//    ve           Venda                                   ve-ZA
//    vi           Vietnamese                              vi-VN
//    vo           Volapük                                 vo-001
//    vun          Vunjo                                   vun-TZ
//    wae          Walser                                  wae-CH
//    wal          Wolaytta                                wal-ET
//    wo           Wolof                                   wo-SN
//    xh           isiXhosa                                xh-ZA
//    xog          Soga                                    xog-UG
//    yav          Yangben                                 yav-CM
//    yi           Yiddish                                 yi-001
//    yo           Yoruba                                  yo-NG
//    zgh          Standard Moroccan Tamazight             zgh-Tfng-MA
//    zgh-Tfng     Standard Moroccan Tamazight (Tifinagh)  zgh-Tfng-MA
//    zh           Chinese                                 zh-CN
//    zh-CHS       Chinese (Simplified) Legacy             zh-CN
//    zh-CHT       Chinese (Traditional) Legacy            zh-HK
//    zh-Hans      Chinese (Simplified)                    zh-CN
//    zh-Hant      Chinese (Traditional)                   zh-HK
//    zu           isiZulu                                 zu-ZA
// The example displays output like the following on a Unix-based system. The output has been cropped for brevity.
//    CULTURE                                              SPECIFIC CULTURE
//                 Invariant Language (Invariant Country)
//    af           Afrikaans                               af-ZA
//    agq          Aghem                                   agq-CM
//    ak           Akan                                    ak-GH
//    am           Amharic                                 am-ET
//    ar           Arabic                                  ar-SA
//    as           Assamese                                as-IN
//    asa          Asu                                     asa-TZ
//    ast          Asturian                                ast-ES
//    az           Azerbaijani                             az-Latn-AZ
//    az-Cyrl      Azerbaijani                             az-Cyrl-AZ
//    az-Latn      Azerbaijani                             az-Latn-AZ
//    bas          Basaa                                   bas-CM
//    be           Belarusian                              be-BY
//    bem          Bemba                                   bem-ZM
//    bez          Bena                                    bez-TZ
//    bg           Bulgarian                               bg-BG
//    bm           Bambara                                 bm-Latn-ML
//    bn           Bangla                                  bn-BD
//    bo           Tibetan                                 bo-CN
//    br           Breton                                  br-FR
//    brx          Bodo                                    brx-IN
//    bs           Bosnian                                 bs-Latn-BA
//    bs-Cyrl      Bosnian                                 bs-Cyrl-BA
//    bs-Latn      Bosnian                                 bs-Latn-BA
//    ca           Catalan                                 ca-ES
//    ccp          Chakma
//    ce           Chechen                                 ce-RU
//    cgg          Chiga                                   cgg-UG
//    chr          Cherokee                                chr-Cher-US
//    ckb          Central Kurdish
//    cs           Czech                                   cs-CZ
//    cy           Welsh                                   cy-GB
//    da           Danish                                  da-DK
//    dav          Taita                                   dav-KE
//    de           German                                  de-DE
//    dje          Zarma                                   dje-NE
//    dsb          Lower Sorbian                           dsb-DE
//    dua          Duala                                   dua-CM
//    ...
//    ta           Tamil                                   ta-IN
//    te           Telugu                                  te-IN
//    teo          Teso                                    teo-UG
//    tg           Tajik                                   tg-Cyrl-TJ
//    th           Thai                                    th-TH
//    ti           Tigrinya                                ti-ER
//    to           Tongan                                  to-TO
//    tr           Turkish                                 tr-TR
//    tt           Tatar                                   tt-RU
//    twq          Tasawaq                                 twq-NE
//    tzm          Central Atlas Tamazight                 tzm-Latn-DZ
//    ug           Uyghur                                  ug-CN
//    uk           Ukrainian                               uk-UA
//    ur           Urdu                                    ur-PK
//    uz           Uzbek                                   uz-Latn-UZ
//    uz-Arab      Uzbek                                   uz-Arab-AF
//    uz-Cyrl      Uzbek                                   uz-Cyrl-UZ
//    uz-Latn      Uzbek                                   uz-Latn-UZ
//    vai          Vai                                     vai-Vaii-LR
//    vai-Latn     Vai                                     vai-Latn-LR
//    vai-Vaii     Vai                                     vai-Vaii-LR
//    vi           Vietnamese                              vi-VN
//    vun          Vunjo                                   vun-TZ
//    wae          Walser                                  wae-CH
//    wo           Wolof                                   wo-SN
//    xog          Soga                                    xog-UG
//    yav          Yangben                                 yav-CM
//    yi           Yiddish                                 yi-001
//    yo           Yoruba                                  yo-NG
//    yue          Cantonese                               yue-HK
//    yue-Hans     Cantonese
//    yue-Hant     Cantonese
//    zgh          Standard Moroccan Tamazight             zgh-Tfng-MA
//    zh           Chinese                                 zh-CN
//    zh-Hans      Chinese                                 zh-CN
//    zh-Hant      Chinese                                 zh-HK
//    zu           Zulu                                    zu-ZA

備註

方法 CreateSpecificCulture 會包裝對建構函式的 CultureInfo(String) 呼叫。

備註

如需 Windows 系統上預先定義文化特性名稱的清單,請參閱 Windows 支援的語言/區域名稱清單中的語言標籤數據行。 文化名稱遵循 BCP 47 定義的標準。 此外,從 Windows 10 開始, name 可以是任何有效的 BCP-47 語言標記。

文化特性分成三組:不因文化特性而異、中性文化特性和特定文化特性。 如需詳細資訊,請參閱 類別的描述 CultureInfo

如果此方法所傳回之特定文化特性的文化特性標識碼符合目前 Windows 文化特性的文化特性標識碼,這個方法會 CultureInfo 建立使用 Windows 文化特性覆寫的物件。 覆寫包含屬性所DateTimeFormat傳回之物件屬性DateTimeFormatInfo的用戶設定,以及 NumberFormatInfo 屬性所NumberFormat傳回的物件。 若要具現化CultureInfo具有預設文化特性設定而非使用者覆寫的物件,請使用 自useUserOverride變數的值false呼叫CultureInfo(String, Boolean)建構函式。

CreateSpecificCulture雖然方法名稱包含「特定」一詞,但請記住文化特性數據可以在版本之間變更,或因自定義文化特性而變更,或因使用者覆寫而變更。 使用不因文化特性或二進位或固定形式來儲存數據。

給呼叫者的注意事項

如果 不是有效的文化特性名稱,.NET Framework 3.5 和舊版會擲回 ArgumentExceptionname 。 從 .NET Framework 4 開始,這個方法會 CultureNotFoundException擲回 。

在 .NET 6 和更新版本上, CultureNotFoundException 如果應用程式是在啟用全球化非變異模式的環境中執行,則會擲回 ,例如,某些 Docker 容器,以及指定非變異文化特性以外的文化特性。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱