Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

SPUserResource class

Represents a user-defined localizable resource.

System.Object
  Microsoft.SharePoint.SPUserResource

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
[SubsetCallableTypeAttribute]
public sealed class 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.

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("http://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();
        }
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.