HOW TO:建立自訂文化特性
更新:2007 年 11 月
由 .NET Framework 和 Windows 作業系統提供的預先定義文化特性,提供如國家/區域使用的語言和月曆等資訊,以及用來格式化、剖析和比較字串、日期及數字的文字慣例。不過,如果預先定義的文化特性不能提供所需要的資訊,您的應用程式可以建立自訂的文化特性。
定義和建立自訂文化特性
應用程式應使用 CultureAndRegionInfoBuilder 物件來定義和命名自訂的文化特性。自訂文化特性可以是全新的文化特性,或是用來取代現有 .NET Framework 文化特性或 Windows 地區設定 (Locale) 的文化特性。
對於新的文化特性,您的應用程式可以用現有的 CultureInfo 物件和 LoadDataFromCultureInfo 方法,以及現有的 RegionInfo 物件和 LoadDataFromRegionInfo 方法,填入 CultureAndRegionInfoBuilder 物件屬性。對於取代的文化特性,CultureAndRegionInfoBuilder 物件屬性會由被取代之文化特性的屬性自動填入。
應用程式會修改 CultureAndRegionInfoBuilder 物件的屬性來滿足需求。
應用程式會呼叫 Register 方法來註冊自訂文化特性。註冊程序會執行以下工作:
建立包含 CultureAndRegionInfoBuilder 物件所定義資訊的 .nlp 檔案。
在電腦上的 %WINDIR%\Globalization 系統目錄儲存 .nlp 檔案,即使關閉電腦,文化特性資訊也會在電腦上持續存在。由於 .nlp 檔案儲存在系統目錄中,因此您必須在註冊自訂文化特性的電腦上擁有系統管理權限。
準備 .NET Framework,在下次需要建立您自己的自訂文化特性時,搜尋 %WINDIR%\Globalization 系統目錄,而非內部快取。
現在,您的自訂文化特性具有和電腦上任何預先定義 .NET Framework 文化特性或 Windows 地區設定相同的狀態。在您的應用程式以 Unregister 方法移除對應的 .nlp 檔案之前,都能夠使用自訂的文化特性。
應用程式會在 CultureInfo 類別建構函式中指定自訂文化特性的名稱。
範例
下列程式碼範例使用 CultureAndRegionInfoBuilder 類別來定義和註冊名為 "x-en-US-sample" 的自訂文化特性,並為這個自訂文化特性建構 CultureInfo 物件。這個範例也會示範 CultureAndRegionInfoBuilder 和 CultureInfo 物件的一些對應屬性。
' This example demonstrates the System.Globalization.Culture-
' AndRegionInfoBuilder Register method.
' Compile this code example with a reference to sysglobl.dll.
Imports System
Imports System.Globalization
Class Sample
Public Shared Sub Main()
Dim cib As CultureAndRegionInfoBuilder = Nothing
Try
' Create a CultureAndRegionInfoBuilder object named "x-en-US-sample".
Console.WriteLine("Create and explore the CultureAndRegionInfoBuilder..." & vbCrLf)
cib = New CultureAndRegionInfoBuilder("x-en-US-sample", CultureAndRegionModifiers.None)
' Populate the new CultureAndRegionInfoBuilder object with culture information.
Dim ci As New CultureInfo("en-US")
cib.LoadDataFromCultureInfo(ci)
' Populate the new CultureAndRegionInfoBuilder object with region information.
Dim ri As New RegionInfo("US")
cib.LoadDataFromRegionInfo(ri)
' Display some of the properties of the CultureAndRegionInfoBuilder object.
Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName)
Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName)
Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName)
Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId)
Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric)
Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol)
Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName)
Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName)
Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName)
Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName)
Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName)
Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName)
Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName)
Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName)
Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName)
Console.WriteLine()
' Register the custom culture.
Console.WriteLine("Register the custom culture...")
cib.Register()
' Display some of the properties of the custom culture.
Console.WriteLine("Create and explore the custom culture..." & vbCrLf)
ci = New CultureInfo("x-en-US-sample")
Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name)
Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName)
Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName)
Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName)
Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName)
Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName)
Console.WriteLine(vbCrLf & "Note:" & vbCrLf & "Use the example in the " & _
"Unregister method topic to remove the custom culture.")
Catch e As Exception
Console.WriteLine(e)
End Try
End Sub 'Main
End Class 'Sample
'This code example produces the following results:
'
'Create and explore the CultureAndRegionInfoBuilder...
'
'CultureName:. . . . . . . . . . x-en-US-sample
'CultureEnglishName: . . . . . . English (United States)
'CultureNativeName:. . . . . . . English (United States)
'GeoId:. . . . . . . . . . . . . 244
'IsMetric: . . . . . . . . . . . False
'ISOCurrencySymbol:. . . . . . . USD
'RegionEnglishName:. . . . . . . United States
'RegionName: . . . . . . . . . . x-en-US-sample
'RegionNativeName: . . . . . . . United States
'ThreeLetterISOLanguageName: . . eng
'ThreeLetterISORegionName: . . . USA
'ThreeLetterWindowsLanguageName: ENU
'ThreeLetterWindowsRegionName: . USA
'TwoLetterISOLanguageName: . . . en
'TwoLetterISORegionName: . . . . US
'
'Register the custom culture...
'Create and explore the custom culture...
'
'Name: . . . . . . . . . . . . . x-en-US-sample
'EnglishName:. . . . . . . . . . English (United States)
'NativeName: . . . . . . . . . . English (United States)
'TwoLetterISOLanguageName: . . . en
'ThreeLetterISOLanguageName: . . eng
'ThreeLetterWindowsLanguageName: ENU
'
'Note:
'Use the example in the Unregister method topic to remove the custom culture.
'
// This example demonstrates the System.Globalization.Culture-
// AndRegionInfoBuilder Register method.
// Compile this code example with a reference to sysglobl.dll.
using System;
using System.Globalization;
class Sample
{
public static void Main()
{
CultureAndRegionInfoBuilder cib = null;
try
{
// Create a CultureAndRegionInfoBuilder object named "x-en-US-sample".
Console.WriteLine("Create and explore the CultureAndRegionInfoBuilder...\n");
cib = new CultureAndRegionInfoBuilder(
"x-en-US-sample", CultureAndRegionModifiers.None);
// Populate the new CultureAndRegionInfoBuilder object with culture information.
CultureInfo ci = new CultureInfo("en-US");
cib.LoadDataFromCultureInfo(ci);
// Populate the new CultureAndRegionInfoBuilder object with region information.
RegionInfo ri = new RegionInfo("US");
cib.LoadDataFromRegionInfo(ri);
// Display some of the properties of the CultureAndRegionInfoBuilder object.
Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName);
Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName);
Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName);
Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId);
Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric);
Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol);
Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName);
Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName);
Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName);
Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName);
Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName);
Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName);
Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName);
Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName);
Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName);
Console.WriteLine();
// Register the custom culture.
Console.WriteLine("Register the custom culture...");
cib.Register();
// Display some of the properties of the custom culture.
Console.WriteLine("Create and explore the custom culture...\n");
ci = new CultureInfo("x-en-US-sample");
Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name);
Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName);
Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName);
Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName);
Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName);
Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName);
Console.WriteLine("\nNote:\n" +
"Use the example in the Unregister method topic to remove the custom culture.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
/*
This code example produces the following results:
Create and explore the CultureAndRegionInfoBuilder...
CultureName:. . . . . . . . . . x-en-US-sample
CultureEnglishName: . . . . . . English (United States)
CultureNativeName:. . . . . . . English (United States)
GeoId:. . . . . . . . . . . . . 244
IsMetric: . . . . . . . . . . . False
ISOCurrencySymbol:. . . . . . . USD
RegionEnglishName:. . . . . . . United States
RegionName: . . . . . . . . . . x-en-US-sample
RegionNativeName: . . . . . . . United States
ThreeLetterISOLanguageName: . . eng
ThreeLetterISORegionName: . . . USA
ThreeLetterWindowsLanguageName: ENU
ThreeLetterWindowsRegionName: . USA
TwoLetterISOLanguageName: . . . en
TwoLetterISORegionName: . . . . US
Register the custom culture...
Create and explore the custom culture...
Name: . . . . . . . . . . . . . x-en-US-sample
EnglishName:. . . . . . . . . . English (United States)
NativeName: . . . . . . . . . . English (United States)
TwoLetterISOLanguageName: . . . en
ThreeLetterISOLanguageName: . . eng
ThreeLetterWindowsLanguageName: ENU
Note:
Use the example in the Unregister method topic to remove the custom culture.
*/