如何:创建自定义区域性
更新:2010 年 12 月
.NET Framework 和 Windows 操作系统附带的预定义区域性指定某个国家/地区中使用的语言和日历,以及在对字符串、日期和数字进行格式设置、分析和比较时使用的文本约定。 但是,如果预定义的区域性未提供所需信息,则您可以创建自定义区域性。
定义并创建自定义区域性
使用 CultureAndRegionInfoBuilder 对象来定义和命名自定义区域性。 自定义区域性可以是全新的区域性、基于现有区域性的新区域性或将替换现有的 .NET Framework 区域性或 Windows 区域设置的区域性。
对于新的区域性,可以通过调用 CultureAndRegionInfoBuilder.LoadDataFromCultureInfo 方法用区域性信息填充 CultureAndRegionInfoBuilder 对象属性,也可以通过调用 CultureAndRegionInfoBuilder.LoadDataFromRegionInfo 方法用区域信息来填充它。 对于替换区域性,被替换的区域性的属性会自动填充 CultureAndRegionInfoBuilder 对象的属性。
根据需要修改 CultureAndRegionInfoBuilder 对象的属性。
调用 CultureAndRegionInfoBuilder.Register 方法以注册自定义区域性。 在注册过程中,将会执行下列操作:
创建一个 . nlp 文件,其中包含在 CultureAndRegionInfoBuilder 对象中定义的信息。
将 . nlp 文件存储在计算机上的 %windir%\Globalization 系统目录中。 这可使区域性信息在计算机上持久保存,即使关闭计算机也不会丢失。 您必须具有此计算机上的管理权限,因为 . nlp 文件存储在系统目录中。
准备 .NET Framework,以便在下次请求创建新的自定义区域性时,搜索 %windir%\Globalization 系统目录而不是内部缓存。
警告 如果您没有对要在其上注册自定义区域性的计算机的管理权限,则调用 CultureAndRegionInfoBuilder.Register 方法将引发 UnauthorizedAccessException。可使用反射调用私有方法来满足此要求。有关更多信息,请参见如何:在没有管理特权的情况下保存自定义区域性。
现在,您的自定义区域性与计算机上的任何预定义 .NET Framework 区域性或 Windows 区域设置都具有相同的状态。 在对 CultureAndRegionInfoBuilder.Unregister 方法的调用从本地计算机中删除 . nlp 文件之前,自定义区域是可用的。
若要基于自定义区域性实例化 CultureInfo 对象,请在 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.
*/
请参见
参考
其他资源
修订记录
日期 |
修订记录 |
原因 |
---|---|---|
2010 年 12 月 |
添加了有关在不具有管理权限的情况下保存自定义区域性的说明。 |
信息补充。 |