Partilhar via


SPUserResource Class

Represents a user-defined localizable resource.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPUserResource

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<SubsetCallableTypeAttribute> _
Public NotInheritable Class SPUserResource
'Usage
Dim instance As SPUserResource
[SubsetCallableTypeAttribute]
public sealed class SPUserResource

Remarks

A user resource is a string that contains text that is used by the multilingual user interface (MUI). For example, a resource might represent the description of a Web site. In this case, a single SPUserResource object representing the description would contain text used for each of the cultures supported by the Web site.

When a new resource is created, the text for all cultures is in the default language of the Web site. Subsequently, someone can translate the text for a particular culture and add the translation to the resource, replacing the untranslated text. One way to do that is to set the Thread.CurrentUICulture property of the currently executing thread to the desired culture and then set the resource object's Value property to the localized string. Another technique is to call the SetValueForUICulture method, specifying the culture and the localized value.

Examples

The following example is a console application that prints data from the SPUserResource object returned by the TitleResource property of an SPWeb object. The application first prints the name of the language associated with the executing thread and the corresponding resource value. Then it prints the name of the default language for the Web site and its corresponding resource value. Finally, if MUI is enabled for the Web site, the application prints the names and resource values for each alternate language that the site supports.

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    // Get the user resource for the Web site's title.
                    SPUserResource resource = web.TitleResource;

                    string format = "Language: {0} | Value: {1}";

                    // Display the value of the resource in the language of the current thread.
                    Console.WriteLine("Current Thread");
                    Console.WriteLine(format, CultureInfo.CurrentCulture.Name, resource.Value);

                    // Display the value for the default language.
                    CultureInfo uiDefault = web.UICulture;
                    Console.WriteLine("\nWeb Site Default");
                    Console.WriteLine(format, uiDefault.Name, resource.GetValueForUICulture(uiDefault));

                    if (web.IsMultilingual)
                    {
                        // Display the value of the resource for each supported language.
                        Console.WriteLine("\nAlternate Languages");

                        IEnumerable<CultureInfo> cultures = web.SupportedUICultures;
                        foreach (CultureInfo culture in cultures)
                        {
                            if (culture.LCID == uiDefault.LCID)
                                continue;

                            string value = resource.GetValueForUICulture(culture);
                            Console.WriteLine(format, culture.Name, value);
                        }
                    }
                    else
                    {
                        Console.WriteLine("\nMUI is not enabled.");
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.Read();
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.OpenWeb()

                ' Get the user resource for the Web site's title.
                Dim resource As SPUserResource = web.TitleResource

                Dim format As String = "Language: {0} | Value: {1}"

                ' Display the value of the resource in the language of the current thread.
                Console.WriteLine("Current Thread")
                Console.WriteLine(format, CultureInfo.CurrentCulture.Name, resource.Value)

                ' Display the value for the default language.
                Dim uiDefault As CultureInfo = web.UICulture
                Console.WriteLine(vbLf & "Web Site Default")
                Console.WriteLine(format, uiDefault.Name, resource.GetValueForUICulture(uiDefault))

                If web.IsMultilingual Then
                    ' Display the value of the resource for each supported language.
                    Console.WriteLine(vbLf & "Alternate Languages")

                    Dim cultures As IEnumerable(Of CultureInfo) = web.SupportedUICultures
                    For Each culture As CultureInfo In cultures
                        If culture.LCID = uiDefault.LCID Then
                            Continue For
                        End If

                        Dim value As String = resource.GetValueForUICulture(culture)
                        Console.WriteLine(format, culture.Name, value)
                    Next
                Else
                    Console.WriteLine(vbLf & "MUI is not enabled.")
                End If

            End Using
        End Using
        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub
End Module

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPUserResource Members

Microsoft.SharePoint Namespace

DescriptionResource

NameResource

DescriptionResource

TitleResource

SPList.DescriptionResource

SPList.TitleResource

SPList.UserResources

TitleResource

CommandUIExtensionResource

DescriptionResource

TitleResource

SPWeb.DescriptionResource

SPWeb.TitleResource

SPWeb.UserResources

SPWeb.SupportedUICultures

Other Resources

Understanding the Multilingual User Interface (MUI)