Description
The SPUtility.GetLocalizedString method retrieves the value of a resource from a specific localized resource file. The method accepts an parameter that identifies the named resource, the root portion of the resource files to use, and the language-specific version to retrieve. The resource files are stored in the <Program Files>\Common Files\Microsoft Shared\web server extensions\12\Resources folder (for example, C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Resources). You can create your own resource files in this location and use them in your code.
Usage Scenario
You will typically use the SPUtility.GetLocalizedString method to retrieve locale-specific strings and other resources (such as bitmaps and files) when your code must support multi-lingual scenarios. For example, you might create three resources files (in the above location) named:
- Messages.resx
- Messages.en-US.resx
- Messages.fr-FR.resx
You might then add a resource called Greeting to each of these files. In the Messages.resx file you might enter a value of 'Hello'; in the Messages.en-US.resx file you might use the value 'Howdy'; and in the Messages.fr-FR.resx, you might add the value 'Bonjour'. When you call the SPUtility.GetLocalizedString method, you would specify $Resources:Greeting as the first parameter, Messages as the second parameter (to represent the root protion of the resource file names) and a language identifier to specify which resource file is to be used.
The following sample shows how to call the SPUtility.GetLocalizedString method, and pass in the language of the current Web site as the language to be used. If the current language is US-English the value 'Howdy' will be retrieved, whereas if the language is French-French, the value 'Bonjour' will be retrieved. If the language is anything else, the value 'Hello' will be retrieved.
C# Code Sample
try
{
string source = "$Resources:Greeting";
string resourceFile = "Messages";
SPWeb thisWeb = SPControl.GetContextWeb(Context);
string msg = SPUtility.GetLocalizedString(source, resourceFile, thisWeb.Language);
}
catch (Exception ex)
{
// handle exception
}
Visual Basic.NET Code Sample
Try
Dim source As String = "$Resources:Greeting"
Dim resourceFile As String = "Messages"
Dim thisWeb As SPWeb = SPControl.GetContextWeb(Context)
Dim msg As String = SPUtility.GetLocalizedString(source, resourceFile, thisWeb.Language)
Catch ex As Exception
' handle exception
End Try