SPUserResource 类

表示的用户定义的本地化资源。

继承层次结构

System.Object
  Microsoft.SharePoint.SPUserResource

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public NotInheritable Class SPUserResource
用法
Dim instance As SPUserResource
public sealed class SPUserResource

备注

用户资源是一个字符串,包含多语言用户界面 (MUI) 使用的文本。例如,资源可能表示网站的说明。在这种情况下,表示说明的单个SPUserResource对象将包含用于每个网站支持的区域性的文本。

创建新资源时,所有区域性的文本是网站的默认语言。随后,某人可以翻译特定区域性的文本,并添加转换到的资源替换未翻译的文本。若要执行此操作的一种方法是将当前执行线程Thread.CurrentUICulture属性设置为所需的区域性,然后将该资源对象的Value属性设置为本地化的字符串。另一种方法是调用SetValueForUICulture方法中,指定区域性和本地化的值。

示例

下面的示例是控制台应用程序打印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

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPUserResource 成员

Microsoft.SharePoint 命名空间

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)