NeutralResourcesLanguageAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
通知应用程序默认区域性的资源控制器。 此类不能被继承。
public ref class NeutralResourcesLanguageAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)]
public sealed class NeutralResourcesLanguageAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NeutralResourcesLanguageAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)>]
type NeutralResourcesLanguageAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type NeutralResourcesLanguageAttribute = class
inherit Attribute
Public NotInheritable Class NeutralResourcesLanguageAttribute
Inherits Attribute
- 继承
- 属性
示例
以下示例使用一个简单的“Hello World”应用来说明如何使用 NeutralResourcesLanguageAttribute 属性来定义默认或回退区域性。 它要求为英语 (en) 、英语 (美国) (en-US) 和法语 (法国) (fr-FR) 区域性创建单独的资源文件。 下面显示了英语区域性名为 ExampleResources.txt 的文本文件的内容。
# Resources for the default (en) culture.
Greeting=Hello
若要在应用中使用资源文件,必须使用 资源文件生成器 (Resgen.exe) 将文件从其文本 (.txt) 格式转换为二进制 (.resources) 格式,如下所示:
resgen ExampleResources.txt
编译应用时,二进制资源文件将嵌入主应用程序集中。
下面显示了名为 ExampleResources.en-US.txt 的文本文件的内容,该文件为英语 (美国) 区域性提供资源。
# Resources for the en-US culture.
Greeting=Hi
可以在命令行中使用 资源文件生成器 (ResGen.exe) 将文本文件转换为二进制资源文件,如下所示:
resgen ExampleResources.en-US.txt ExampleResources.en-US.resources
然后,应使用 程序集链接器 (Al.exe) 将二进制资源文件编译为程序集,并通过发出以下命令放置在应用目录的 en-US 子目录中:
al /t:lib /embed:ExampleResources.en-US.resources /culture:en-US /out:en-us\Example.resources.dll
下面显示了名为 ExampleResources.fr-FR.txt 的文本文件的内容,该文件为法语 (法国) 区域性提供资源。
# Resources for the fr-FR culture.
Greeting=Bonjour
可以使用命令行中的 ResGen.exe 将文本文件转换为二进制资源文件,如下所示:
resgen ExampleResources.fr-FR.txt ExampleResources.fr-FR.resources
然后,应使用程序集链接器将二进制资源文件编译为程序集,并通过发出以下命令将其放置在应用目录的 fr-FR 子目录中:
al /t:lib /embed:ExampleResources.fr-FR.resources /culture:fr-FR /out:fr-FR\Example.resources.dll
以下示例提供可执行代码,用于设置当前区域性、提示输入用户名并显示本地化字符串。
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;
[assembly:NeutralResourcesLanguageAttribute("en")]
public class Example
{
public static void Main()
{
// Select the current culture randomly to test resource fallback.
string[] cultures = { "de-DE", "en-us", "fr-FR" };
Random rnd = new Random();
int index = rnd.Next(0, cultures.Length);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultures[index]);
Console.WriteLine("The current culture is {0}",
CultureInfo.CurrentUICulture.Name);
// Retrieve the resource.
ResourceManager rm = new ResourceManager("ExampleResources" ,
typeof(Example).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading
<Assembly:NeutralResourcesLanguageAttribute("en")>
Module Example
Public Sub Main()
' Select the current culture randomly to test resource fallback.
Dim cultures() As String = { "de-DE", "en-us", "fr-FR" }
Dim rnd As New Random()
Dim index As Integer = rnd.Next(0, cultures.Length)
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultures(index))
Console.WriteLine("The current culture is {0}",
CultureInfo.CurrentUICulture.Name)
' Retrieve the resource.
Dim rm As New ResourceManager("ExampleResources" , GetType(Example).Assembly)
Dim greeting As String = rm.GetString("Greeting")
Console.Write("Enter your name: ")
Dim name As String = Console.ReadLine()
Console.WriteLine("{0} {1}", greeting, name)
End Sub
End Module
可以在 Visual Basic 中使用以下命令编译它:
vbc Example.vb /resource:ExampleResources.resources
或在 C# 中使用以下命令:
csc Example.cs /resource:ExampleResources.resources
注解
桌面应用
在桌面应用中, NeutralResourcesLanguageAttribute 属性通知资源管理器应用的默认区域性及其资源的位置。 默认情况下,资源嵌入在主应用程序集中,你可以使用 属性,如下所示。 此语句指定英语 (美国) 是应用的默认区域性。
using System.Resources;
[assembly:NeutralResourcesLanguage("en-US")]
Imports System.Resources
<Assembly:NeutralResourcesLanguage("en-US")>
还可以使用 NeutralResourcesLanguageAttribute 特性来指示可在何处 ResourceManager 找到默认区域性的资源,方法是在 attribute 语句中提供 UltimateResourceFallbackLocation 枚举值。 这通常是为了指示资源驻留在附属程序集中。 例如,下面的语句指定英语 (美国) 是应用的默认区域性或非特定区域性,并且其资源驻留在附属程序集中。 对象 ResourceManager 将在名为 en-US 的子目录中查找它们。
using System.Resources;
[assembly:NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
Imports System.Resources
<Assembly:NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>
提示
建议始终使用 NeutralResourcesLanguageAttribute 属性来定义应用的默认区域性。
属性执行两个角色:
如果默认区域性的资源嵌入在应用的主程序集中,并且 ResourceManager 必须检索与默认区域性属于同一区域性的资源,则 ResourceManager 会自动使用位于主程序集中的资源,而不是搜索附属程序集。 这样可绕过常用程序集探测,提高所加载的第一个资源的查找性能,并可缩小工作集。 有关用于探测资源文件的过程ResourceManager,请参阅打包和部署资源。
如果默认区域性的资源位于附属程序集而不是主应用程序集中,则 NeutralResourcesLanguageAttribute 属性指定运行时可从中加载资源的区域性和目录。
Windows 8.x 应用商店应用
在使用 类加载和检索ResourceManager资源的 Windows 8.x 应用商店应用中, NeutralResourcesLanguageAttribute 属性定义在探测失败时使用其资源的非特定区域性。 它未指定资源的位置。 默认情况下, ResourceManager 使用应用的包资源索引 (PRI) 文件来查找默认区域性的资源。 由 NeutralResourcesLanguageAttribute 属性定义的非特定区域性将添加到 UI 语言列表的末尾,以模拟此效果。
如果使用 Windows 运行时Windows.ApplicationModel.Resources.ResourceLoader 类或 Windows.ApplicationModel.Resources.Core 命名空间中的类型加载和检索资源,则会忽略 该NeutralResourcesLanguageAttribute属性。
构造函数
属性
CultureName |
获取区域性名称。 |
Location |
获取 ResourceManager 类的位置,用于通过资源回退进程检索非特定语言资源。 |
TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。 (继承自 Attribute) |
方法
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
GetHashCode() |
返回此实例的哈希代码。 (继承自 Attribute) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |