Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPUtility Class
SPUtility Methods
 GetLocalizedString Method

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
SPUtility.GetLocalizedString Method (Microsoft.SharePoint.Utilities)

Namespace: Microsoft.SharePoint.Utilities
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Shared Function GetLocalizedString ( _
    source As String, _
    defaultResourceFile As String, _
    language As UInteger _
) As String
Visual Basic (Usage)
Dim source As String
Dim defaultResourceFile As String
Dim language As UInteger
Dim returnValue As String

returnValue = SPUtility.GetLocalizedString(source, defaultResourceFile, language)
C#
public static string GetLocalizedString (
    string source,
    string defaultResourceFile,
    uint language
)

Parameters

source

defaultResourceFile

language

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
More explanations      Gat   |   Edit   |   Show History

This method retrieve resources from RESX files stored into the 12\Resources folder.

source : the full name of the resource to retrieve (example : "$Resources:Blog_Title")

defaultResourceFile : base name of the resource file (example : "wss" for wss.resx / wss.en-US.res / wss.fr-FR.rex ...)

language : LCID used for localization

---------------------

Sample code that display the resource called "LocalizationWorks" included into resources files called "myresources.resx" and "myresources.fr-FR.resx" using the current context (current website's language) :

string textToDisplay = SPUtility.GetLocalizedString("$Resources:LocalizationWorks", "myresources", SPContext.Current.Web.Language);
this.label1.Text = textToDisplay;

Tags What's this?: Add a tag
Flag as ContentBug
SPUtility.GetLocalizedString      Content Master Ltd   |   Edit   |   Show History

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

Unable to get text from wss.resx      damian98   |   Edit   |   Show History
Due to the fact that the central wss.resx file is NOT stored in 12\Resources it's not possible to get any text from wss.resx via the GetLocalizedString method.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker