Export (0) Print
Expand All

SPUserResource class

Represents a user-defined localizable resource.

System.Object
  Microsoft.SharePoint.SPUserResource

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

'Declaration
Public NotInheritable Class SPUserResource
'Usage
Dim instance As SPUserResource

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.

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.

Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("http://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

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