Export (0) Print
Expand All
Expand Minimize

SPUtility.GetLocalizedString method

Retrieves the value for a named resource string from the resource file for a specified language.

Namespace:  Microsoft.SharePoint.Utilities
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public static string GetLocalizedString(
	string source,
	string defaultResourceFile,
	uint language
)

Parameters

source
Type: System.String

An ASP.NET resource expression in the form $Resources:keyname, where keyname is the name half of a name/value pair in a resource file (.resx).

defaultResourceFile
Type: System.String

The base file name of the language resource file containing a localized string value. For example, if you have a series of resource files named myresources.en-us.resx, myresources.es-es.resx, myresources.de-de.resx, and so on, the value to pass in this parameter is myresources.

language
Type: System.UInt32

The LCID of the target language. For a list, see the overview topic for the CultureInfo class.

Return value

Type: System.String
The string value for the specified language. If a value cannot be found in the requested language, the value for the invariant language is returned. If a resource file for the invariant language does not exist or the specified resource name does not exist, the source string is returned without localization.

This method can retrieve a string value from a resource file located in the Resources folder that is just below the SharePoint Foundation installation root. The token that Visual Studio uses for this folder is {SharePointRoot}\Resources. The full path is %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\Resources.

The following example is a console application that enumerates the languages supported by a Web site. For each language, the application gets the value for the resource named "onet_TeamSite" from the resource file with the base file name "core".

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    if (web.IsMultilingual)
                    {
                        IEnumerable<CultureInfo> cultures = web.SupportedUICultures;
                        foreach (CultureInfo culture in cultures)
                        {
                            // Print the value of a language resource in the current language.
                            string str = "$Resources:onet_TeamWebSite";
                            string locStr = SPUtility.GetLocalizedString(str, "core", (uint)culture.LCID);

                            Console.WriteLine("{0}  {1}", culture.Name, locStr);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}
Show:
© 2014 Microsoft