NeutralResourcesLanguageAttribute 类

定义

通知应用程序默认区域性的资源控制器。Informs the resource manager of an app's default culture. 此类不能被继承。This class cannot be inherited.

public ref class NeutralResourcesLanguageAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NeutralResourcesLanguageAttribute : Attribute
type NeutralResourcesLanguageAttribute = class
    inherit Attribute
Public NotInheritable Class NeutralResourcesLanguageAttribute
Inherits Attribute
继承
NeutralResourcesLanguageAttribute
属性

示例

下面的示例使用一个简单的"Hello World"应用来演示如何使用NeutralResourcesLanguageAttribute属性来定义默认或回退区域性。The following example uses a simple "Hello World" app to illustrate the use of the NeutralResourcesLanguageAttribute attribute to define a default or fallback culture. 它为英语 (en)、 英语 (美国) (EN-US) 和法语 (法国) (FR-FR) 需要创建单独的资源文件的区域性。It requires the creation of separate resource files for the English (en), English (United States) (en-US), and French (France) (fr-FR) cultures. 下面显示了名 ExampleResources.txt 为英语区域性的文本文件的内容。The following shows the contents of a text file named ExampleResources.txt for the English culture.

# Resources for the default (en) culture.  
Greeting=Hello  

若要使用的应用程序中的资源文件,必须使用资源文件生成器 (Resgen.exe)将文件从其文本 (.txt) 格式转换为二进制 (.resources) 文件格式,按如下所示:To use the resource file in an app, you must use the Resource File Generator (Resgen.exe) to convert the file from its text (.txt) format to a binary (.resources) format as follows:

resgen ExampleResources.txt  

编译应用程序后,将在主应用程序集中嵌入二进制资源文件。When the app is compiled, the binary resource file will be embedded in the main app assembly.

下面显示了一个名为提供用于英语 (美国) 区域性的资源的 ExampleResources.en US.txt 文本文件的内容。The following shows the contents of a text file named ExampleResources.en-US.txt that provides resources for the English (United States) culture.

# Resources for the en-US culture.  
Greeting=Hi  

该文本文件可以通过使用转换为二进制资源文件资源文件生成器 (ResGen.exe)命令行,如下所示:The text file can be converted to a binary resources file by using the Resource File Generator (ResGen.exe) at the command line as follows:

resgen ExampleResources.en-US.txt ExampleResources.en-US.resources  

二进制资源文件应然后将编译为程序集通过使用程序集链接器 (Al.exe)并放入应用程序目录的 EN-US 子目录通过发出以下命令:The binary resource file should then be compiled into an assembly by using Assembly Linker (Al.exe) and placed in the en-US subdirectory of the app directory by issuing the following command:

al /t:lib /embed:ExampleResources.en-US.resources /culture:en-US /out:en-us\Example.resources.dll  

下面显示了一个名为提供了法语 (法国) 区域性的资源的 ExampleResources.fr FR.txt 文本文件的内容。The following shows the contents of a text file named ExampleResources.fr-FR.txt that provides resources for the French (France) culture.

# Resources for the fr-FR culture.  
Greeting=Bonjour  

该文本文件可以按以下方式在命令行使用 ResGen.exe 转换为二进制资源文件:The text file can be converted to a binary resource file by using ResGen.exe at the command line as follows:

resgen ExampleResources.fr-FR.txt ExampleResources.fr-FR.resources  

二进制资源文件然后应使用程序集链接器编译到程序集并通过发出以下命令放入应用程序目录的-FR 子目录中:The binary resources file should then be compiled into an assembly by using Assembly Linker and placed in the fr-FR subdirectory of the app directory by issuing the following command:

al /t:lib /embed:ExampleResources.fr-FR.resources /culture:fr-FR /out:fr-FR\Example.resources.dll  

下面的示例提供了将当前区域性设置,提示输入用户的名称,并显示的已本地化的字符串的可执行代码。The following example provides the executable code that sets the current culture, prompts for the user's name, and displays a localized string.

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 中使用以下命令编译:It can be compiled by using the following command in Visual Basic:

vbc Example.vb /resource:ExampleResources.resources  

或通过使用以下命令在 C# 中:or by using the following command in C#:

csc Example.cs /resource:ExampleResources.resources  

注解

桌面应用程序Desktop Apps

在桌面应用中,NeutralResourcesLanguageAttribute特性告知应用程序的默认区域性的资源管理器和其资源的位置。In desktop apps, the NeutralResourcesLanguageAttribute attribute informs the resource manager of an app's default culture and the location of its resources. 默认情况下,资源嵌入主应用程序集中,并且可以按如下所示使用的属性。By default, resources are embedded in the main app assembly, and you can use the attribute as follows. 此语句指定英语 (美国) 是应用程序的默认区域性。This statement specifies that the English (United States) is the app's default culture.

using System.Resources;

[assembly:NeutralResourcesLanguage("en-US")]
Imports System.Resources

<Assembly:NeutralResourcesLanguage("en-US")>

此外可以使用NeutralResourcesLanguageAttribute属性来指示何处ResourceManager可以找到默认区域性的资源,从而UltimateResourceFallbackLocation属性语句中的枚举值。You can also use the NeutralResourcesLanguageAttribute attribute to indicate where ResourceManager can find the resources of the default culture by providing an UltimateResourceFallbackLocation enumeration value in the attribute statement. 这通常指示资源位于附属程序集。This is most commonly done to indicate that the resources reside in a satellite assembly. 例如,下面的语句指定英语 (美国) 的应用程序的默认值或非特定区域性,并且其资源位于附属程序集。For example, the following statement specifies that English (United States) is the app's default or neutral culture and that its resources reside in a satellite assembly. ResourceManager对象将为其查找名为 EN-US 子目录中。The ResourceManager object will look for them in a subdirectory named en-US.

using System.Resources;

[assembly:NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
Imports System.Resources

<Assembly:NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>

提示

我们建议始终使用NeutralResourcesLanguageAttribute属性来定义您的应用程序的默认区域性。We recommend that you always use the NeutralResourcesLanguageAttribute attribute to define the default culture of your app.

该属性将执行两个角色:The attribute performs two roles:

  • 如果默认区域性的资源嵌入到应用的主程序集和ResourceManager必须检索属于相同的区域性的默认区域性资源ResourceManager会自动使用而是位于主程序集中的资源搜索附属程序集。If the default culture's resources are embedded in the app's main assembly and ResourceManager has to retrieve resources that belong to the same culture as the default culture, the ResourceManager automatically uses the resources located in the main assembly instead of searching for a satellite assembly. 这会绕过常用的程序集探测,提高了加载,并可以减少您的工作集的第一个资源的查找性能。This bypasses the usual assembly probe, improves lookup performance for the first resource you load, and can reduce your working set. 请参阅打包和部署资源进程ResourceManager用来探测资源文件。See Packaging and Deploying Resources for the process ResourceManager uses to probe for resource files.

  • 如果默认区域性的资源位于附属程序集中,而不是在主应用程序集中,NeutralResourcesLanguageAttribute属性指定的区域性和运行时可以从其加载的资源的目录。If the default culture's resources are located in a satellite assembly rather than in the main app assembly, the NeutralResourcesLanguageAttribute attribute specifies the culture and the directory from which the runtime can load the resources.

Windows 8.x 应用商店Windows 8.x Store 应用程序Apps

在中Windows 8.x 应用商店Windows 8.x Store应用程序加载和使用来检索其资源ResourceManager类,NeutralResourcesLanguageAttribute属性定义了非特定区域性发生失败的探测时使用的资源。In Windows 8.x 应用商店Windows 8.x Store apps whose resources are loaded and retrieved by using the ResourceManager class, the NeutralResourcesLanguageAttribute attribute defines the neutral culture whose resources are used in the event of a failed probe. 它不指定资源的位置。It does not specify the location of the resources. 默认情况下,ResourceManager使用应用程序的包资源索引 (PRI) 文件来查找默认区域性的资源。By default, ResourceManager uses the app's package resource index (PRI) file to locate the resources of the default culture. 由定义的非特定区域性NeutralResourcesLanguageAttribute属性添加到要模拟此效果的 UI 语言列表的末尾。The neutral culture that is defined by the NeutralResourcesLanguageAttribute attribute is added to the end of the UI language list to simulate this effect.

如果加载并使用检索资源Windows 运行时Windows Runtime Windows.ApplicationModel.Resources.ResourceLoader类或类型中的Windows.ApplicationModel.Resources.Core命名空间,NeutralResourcesLanguageAttribute忽略属性。If you load and retrieve resources by using the Windows 运行时Windows RuntimeWindows.ApplicationModel.Resources.ResourceLoader class or the types in the Windows.ApplicationModel.Resources.Core namespace, the NeutralResourcesLanguageAttribute attribute is ignored.

构造函数

NeutralResourcesLanguageAttribute(String)

初始化 NeutralResourcesLanguageAttribute 类的新实例。Initializes a new instance of the NeutralResourcesLanguageAttribute class.

NeutralResourcesLanguageAttribute(String, UltimateResourceFallbackLocation)

使用指定的最终资源后备位置初始化 NeutralResourcesLanguageAttribute 类的新实例。Initializes a new instance of the NeutralResourcesLanguageAttribute class with the specified ultimate resource fallback location.

属性

CultureName

获取区域性名称。Gets the culture name.

Location

获取 ResourceManager 类的位置,该类用于通过资源回退进程检索非特定语言资源。Gets the location for the ResourceManager class to use to retrieve neutral resources by using the resource fallback process.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅