Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

ResourceManager.GetString Method (String, CultureInfo)

Returns the value of the string resource localized for the specified culture.

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Overridable Function GetString ( _
	name As String, _
	culture As CultureInfo _
) As String

Parameters

name
Type: System.String

The name of the resource to retrieve.

culture
Type: System.Globalization.CultureInfo

An object that represents the culture for which the resource is localized.

Return Value

Type: System.String
The value of the resource localized for the specified culture, or Nothing if name cannot be found in a resource set.
ExceptionCondition
ArgumentNullException

The name parameter is Nothing.

InvalidOperationException

The value of the specified resource is not a string.

MissingManifestResourceException

No usable set of resources has been found, and there are no resources for a default culture. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

MissingSatelliteAssemblyException

The default culture's resources reside in a satellite assembly that could not be found. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

Desktop Apps

In desktop apps, if culture is Nothing, the GetString(String, CultureInfo) method uses the current UI culture obtained from the CultureInfo.CurrentUICulture property.

The resource that is returned is localized for the culture specified by the culture parameter. If the resource has not been localized for culture, the resource manager probes for a resource by following the steps outlined in the "Resource Fallback Process" section of the Packaging and Deploying Resources in Desktop Apps topic. If no usable set of resources is found, the resource manager falls back on the default culture's resources. If the resource manager cannot load the default culture's resource set, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns Nothing.

The IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

Caution noteCaution

This method can throw more exceptions than are listed. One reason this might occur is if a method that this method calls throws an exception. For example, a FileLoadException exception might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException exception might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

Windows Store Apps

Important noteImportant

Although the ResourceManagerclass is supported in Windows Store apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows Store apps. To retrieve resources from Windows Store apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.

In Windows Store apps, the GetString(String, CultureInfo) method returns the value of the name string resource, localized for the culture specified by the culture parameter. If the resource is not localized for the culture culture, the lookup uses the entire Windows 8 language fallback list, and stops after looking in the default culture. If the resource manager cannot match name, the method returns Nothing.

The following example uses the GetString(String, CultureInfo) method to retrieve culture-specific resources. The example's default culture is English (en), and it includes satellite assemblies for the French (France) (fr-FR) and Russian (Russia) (ru-RU) cultures. The example changes the current culture and current UI culture to Russian (Russia) before calling GetString(String, CultureInfo). It then calls the GetString method and the DateTime.ToString(String, IFormatProvider) method and passes CultureInfo objects that represent the French (France) and Swedish (Sweden) cultures to each method. In the output, the month and day of the month as well as the string that precedes them appear in French, because the GetString method is able to retrieve the French language resource. However, when the Swedish (Sweden) culture is used, the month and day of the month appear in Swedish, although the string that precedes them is in English. This is because the resource manager cannot find localized Swedish language resources, so it returns a resource for the default English culture instead.

The example requires the text-based resource files listed in following table. Each has a single string resource named DateStart.

Culture

File name

Resource name

Resource value

en-US

DateStrings.txt

DateStart

Today is

fr-FR

DateStrings.fr-FR.txt

DateStart

Aujourd'hui, c'est le

ru-RU

DateStrings.ru-RU.txt

DateStart

Сегодня

You can use the following batch file to compile the Visual Basic example. To compile in C#, change vbc to csc, and change the extension of the source code file from .vb to .cs.

resgen DateStrings.txt
vbc showdate.vb /resource:DateStrings.resources

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

md ru-RU
resgen DateStrings.ru-RU.txt
al /out:ru-RU\Showdate.resources.dll /culture:ru-RU /embed:DateStrings.ru-RU.resources

Here's the source code for the example (ShowDate.vb for the Visual Basic version or ShowDate.cs for the C# version).

Imports System.Globalization
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguage("en")>

Module Example
   Public Sub Main()
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ru-RU")
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ru-RU")

      Dim cultureNames() As String = { "fr-FR", "sv-SE" }
      Dim rm As New ResourceManager("DateStrings",
                                    GetType(Example).Assembly)

      For Each cultureName In cultureNames
         Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
         Dim dateString As String = rm.GetString("DateStart", culture)
         Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString, 
                                            Date.Now.ToString("M", culture))                           
         Console.WriteLine()
      Next                                            
   End Sub 
End Module 
' The example displays output similar to the following: 
'       French (France): Aujourd'hui, c'est le 7 février. 
'        
'       Swedish (Sweden): Today is den 7 februari.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.