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 的文本文件的内容。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 的文本文件的内容,该文件提供英语(美国)区域性的资源。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-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 可以通过在 attribute 语句中提供 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 Runtimewindows.applicationmodel.resources.resourceloader类或windows.applicationmodel.resources.core命名空间中的类型加载和检索资源,则会忽略 NeutralResourcesLanguageAttribute 的属性(attribute)。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()

返回一个表示当前对象的 string。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)

适用于

另请参阅