System.Resources.NeutralResourcesLanguageAttribute 類別

本文提供此 API 參考文件的補充備註。

在傳統型應用程式中, NeutralResourcesLanguageAttribute 屬性會通知資源管理員應用程式的預設文化特性及其資源的位置。 根據預設,資源會內嵌在主要應用程式元件中,您可以使用 屬性,如下所示。 此語句指定英文 (美國) 是應用程式的預設文化特性。

[assembly: NeutralResourcesLanguage("en-US")]
<Assembly:NeutralResourcesLanguage("en-US")>

您也可以使用 NeutralResourcesLanguageAttribute 屬性,藉由在 attribute 語句中提供UltimateResourceFallbackLocation列舉值,以指出哪裡可以找到ResourceManager預設文化特性的資源。 這最常用來指出資源位於附屬元件中。 例如,下列語句指定英文 (美國) 是應用程式的預設或中性文化特性,且其資源位於附屬元件中。 物件 ResourceManager 會在名為 en-US 的子目錄中尋找它們。

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
<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則會忽略 屬性。

範例

下列範例使用簡單的 「Hello World」 應用程式來說明如何使用 NeutralResourcesLanguageAttribute 屬性來定義預設或後援文化特性。 它需要為英文(en)、英文(美國)和法文(法國)(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

接著,二進位資源文件應該使用 Assembly Linker 編譯成元件(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

然後,二進位資源文件應該使用 Assembly Linker 編譯成元件,並透過發出下列命令,放在應用程式目錄的 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.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