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 {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($"{greeting} {name}!");
}
}
Imports System.Globalization
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 属性将通知资源管理器应用的默认区域性及其资源的位置。 默认情况下,资源嵌入在主应用程序集中,可以使用如下所示的属性。 此语句指定英语(美国)是应用的默认语言文化。
[assembly: NeutralResourcesLanguage("en-US")]
<Assembly:NeutralResourcesLanguage("en-US")>
您还可以使用NeutralResourcesLanguageAttribute属性,通过在属性语句中提供ResourceManager枚举值,来指示UltimateResourceFallbackLocation可以在哪里找到默认文化的资源。 这通常是为了指示资源驻留在附属程序集。 例如,以下语句指定英语(美国)是应用的默认或非特定区域性,并且其资源驻留在附属程序集中。 该 ResourceManager 对象将在名为 en-US的子目录中查找它们。
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
<Assembly:NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>
Tip
我们建议您始终使用 NeutralResourcesLanguageAttribute 属性来定义您的应用程序的默认文化。
该属性执行两个角色:
如果默认区域性的资源嵌入在应用的主程序集中,并且 ResourceManager 必须检索与默认区域性属于同一区域性的资源,则 ResourceManager 会自动使用位于主程序集中的资源,而不是搜索附属程序集。 这样可绕过常用程序集探测,提高所加载的第一个资源的查找性能,并可缩小工作集。 请参阅 打包和部署资源 ,了解用于探测资源文件的过程 ResourceManager 。
如果默认文化的资源位于附属程序集而不是主应用程序集中,则 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) |