如何:创建自定义区域性
更新:2007 年 11 月
.NET Framework 和 Windows 操作系统附带的预定义区域性提供的信息包括:某个国家/地区中使用的语言和日历,以及在对字符串、日期和数字进行格式设置、分析和比较时使用的文本约定。但是,如果预定义的区域性未提供所需信息,则应用程序可以创建自定义区域性。
定义并创建自定义区域性
应用程序应该使用 CultureAndRegionInfoBuilder 对象来定义和命名自定义区域性。自定义区域性可以是全新的区域性,也可以是替换现有的 .NET Framework 区域性或 Windows 区域设置的区域性。
对于新的区域性,应用程序可以使用现有的 CultureInfo 对象和 LoadDataFromCultureInfo 方法来填充 CultureAndRegionInfoBuilder 对象属性,也可以使用现有的 RegionInfo 对象和 LoadDataFromRegionInfo 方法进行填充。对于替换区域性,被替换的区域性的属性会自动填充 CultureAndRegionInfoBuilder 对象的属性。
应用程序修改 CultureAndRegionInfoBuilder 对象的属性以满足要求。
应用程序调用 Register 方法来注册自定义区域性。在注册过程中,将会执行下列操作:
创建一个 .nlp 文件,其中包含 CultureAndRegionInfoBuilder 对象中定义的信息。
将 .nlp 文件存储在计算机上的 %WINDIR%\Globalization 系统目录中,这可使区域性在计算机上持久保存,即使关闭计算机也不会丢失。您必须对注册自定义区域性的计算机拥有管理权限,因为 .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.
*/