Share via


SPUserResource class

代表使用者定義的可當地語系化資源。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPUserResource

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

Syntax

'宣告
Public NotInheritable Class SPUserResource
'用途
Dim instance As SPUserResource
public sealed class SPUserResource

備註

使用者資源是包含多語系使用者介面 (MUI) 所使用的文字字串。例如,資源可能會代表網站的描述。在此情況下,單一SPUserResource物件,代表描述會包含用於每個網站所支援的文化特性的文字。

建立新資源時,所有文化特性的文字是在網站的預設語言。後來,某人可翻譯的文字為特定的文化特性,並將翻譯新增至資源,取代原狀的文字。一種方式,若要這樣做會將目前正在執行的執行緒Thread.CurrentUICulture屬性設定為所需的文化特性,然後將資源物件的Value屬性設為當地語系化的字串。另一種技術是呼叫SetValueForUICulture方法,指定文化特性,並使用當地語系化的值。

Examples

下面範例會列印資料從SPWeb物件的TitleResource屬性所傳回的SPUserResource物件的主控台應用程式。應用程式先列印與執行的執行緒及對應的資源值相關聯之語言的名稱。然後它會在列印網站和其對應的資源值的預設語言的名稱。最後,如果網站啟用 MUI,將應用程式會列印的名稱和網站支援每個其他語言的資源值。

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.

請參閱

參照

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

其他資源

Multilingual User Interface (MUI)