Επεξεργασία

Κοινή χρήση μέσω


ResourceManager.GetResourceSet(CultureInfo, Boolean, Boolean) Method

Definition

Retrieves the resource set for a particular culture.

public:
 virtual System::Resources::ResourceSet ^ GetResourceSet(System::Globalization::CultureInfo ^ culture, bool createIfNotExists, bool tryParents);
public virtual System.Resources.ResourceSet? GetResourceSet (System.Globalization.CultureInfo culture, bool createIfNotExists, bool tryParents);
public virtual System.Resources.ResourceSet GetResourceSet (System.Globalization.CultureInfo culture, bool createIfNotExists, bool tryParents);
abstract member GetResourceSet : System.Globalization.CultureInfo * bool * bool -> System.Resources.ResourceSet
override this.GetResourceSet : System.Globalization.CultureInfo * bool * bool -> System.Resources.ResourceSet
Public Overridable Function GetResourceSet (culture As CultureInfo, createIfNotExists As Boolean, tryParents As Boolean) As ResourceSet

Parameters

culture
CultureInfo

The culture whose resources are to be retrieved.

createIfNotExists
Boolean

true to load the resource set, if it has not been loaded yet; otherwise, false.

tryParents
Boolean

true to use resource fallback to load an appropriate resource if the resource set cannot be found; false to bypass the resource fallback process.

Returns

The resource set for the specified culture.

Exceptions

The culture parameter is null.

tryParents is true, no usable set of resources has been found, and there are no default culture resources.

Examples

The following example calls the GetResourceSet method to retrieve culture-specific resources for the French (France) culture. It then enumerates all of the resources in the resource set. It contains the source code for an executable named ShowNumbers.exe. It also includes the following two text file that contain the names of numbers. The first, NumberResources.txt, contains the names of numbers from one to ten in the English language:

one=one
two=two
three=three
four=four
five=five
six=six
seven=seven
eight=eight
nine=nine
ten=ten

The second, NumberResources.fr-FR.txt, contains the names of numbers from one to four in the French language:

one=un
two=deux
three=trois
four=quatre

You can use a batch file to generate the resource files, embed the English language resource file into the executable, and create a satellite assembly for the French language resources. Here's the batch file to generate an executable by using the Visual Basic compiler:

resgen NumberResources.txt
vbc shownumbers.vb /resource:NumberResources.resources

md fr-FR
resgen NumberResources.fr-FR.txt
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources

For the C# compiler, you can use the following batch file:

resgen NumberResources.txt
csc shownumbers.cs /resource:NumberResources.resources

md fr-FR
resgen NumberResources.fr-FR.txt
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources
using System;
using System.Globalization;
using System.Resources;

public class Example
{
   public static void Main()
   {
      String[] numbers = { "one", "two", "three", "four", "five", "six",
                           "seven", "eight", "nine", "ten" };
      var rm = new ResourceManager(typeof(NumberResources));
      ResourceSet rs = rm.GetResourceSet(CultureInfo.CreateSpecificCulture("fr-FR"),
                                         true, false);
      if (rs == null) {
         Console.WriteLine("No resource set.");
         return;
      }

      foreach (var number in numbers)
         Console.WriteLine("{0,-10} '{1}'", number + ":", rs.GetString(number));
   }
}

internal class NumberResources {}
// The example displays the following output:
//       one:       'un'
//       two:       'deux'
//       three:     'trois'
//       four:      'quatre'
//       five:      ''
//       six:       ''
//       seven:     ''
//       eight:     ''
//       nine:      ''
//       ten:       ''
Imports System.Globalization
Imports System.Resources

Module Example
   Public Sub Main()
      Dim numbers() As String = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }
      Dim rm As New ResourceManager(GetType(NumberResources))
      Dim rs As ResourceSet = rm.GetResourceSet(CultureInfo.CreateSpecificCulture("fr-FR"), True, False)
      If rs Is Nothing Then Console.WriteLine("No resource set.") : Exit Sub
      For Each number In numbers
         Console.WriteLine("{0,-10} '{1}'", number + ":", rs.GetString(number))
      Next            
   End Sub
End Module

Public Class NumberResources
End Class
' The example displays the following output:
'       one:       'un'
'       two:       'deux'
'       three:     'trois'
'       four:      'quatre'
'       five:      ''
'       six:       ''
'       seven:     ''
'       eight:     ''
'       nine:      ''
'       ten:       ''

Note that if you change the value of the createIfNotExists argument to false, the method call returns null, since Resource Manager has not already loaded the French language resources.

Remarks

The resource set that is returned represents the resources that are localized for the specified culture. If the resources have not been localized for that culture and tryParents is true, GetResourceSet uses resource fallback rules to load an appropriate resource. If tryParents is false and a culture-specific resource set cannot be found, the method returns null. For more information about resource fallback, see "The Resource Fallback Process" section in the Packaging and Deploying Resources article.

Applies to

See also