Export (0) Print
Expand All
Expand Minimize

SPWeb.ImportUserResources Method

Imports user resources for the specified language.

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

[SubsetCallableExcludeMemberAttribute(SubsetCallableExcludeMemberType.MarshalByRefObject)]
public void ImportUserResources(
	CultureInfo language,
	Stream inputFile
)

Parameters

language
Type: System.Globalization.CultureInfo

Specifies the culture for which resources are to be imported. The value must be one of the cultures in the collection returned by the SupportedUICultures property but it cannot be the default culture for the website's user interface, the value returned by the UICulture property.

inputFile
Type: System.IO.Stream

A stream with the user resource data. The data must be in .Resx file format.

ExceptionCondition
InvalidOperationException

No alternate languages are enabled on the site.

InvalidOperationException

The specified language is not one of the installed languages.

InvalidOperationException

Either the language is not supported by the website, or it is the default language. You cannot import resources for the default language of a website.

InvalidOperationException

The input file cannot be imported because its language does not match the specified language.

You can call the ExportUserResources method to create a resource file for each of the languages supported by the site and then give the files to someone who can translate the strings that they contain. When this work is complete, you can call the ImportUserResources method to import the translations, making them available to the multilingual user interface.

You cannot import resources for the default language of the website. New text for the user interface is created in the default language and is subsequently translated to alternate languages. The default language text is used as the resource value for alternate languages until translations are supplied. For more information, see the OverwriteTranslationsOnChange property.

The following example is a console application that looks for a culture-specific resource file for each of the alternate languages supported by a website. If the application finds a resource file, it imports it.

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    if (web.IsMultilingual)
                    {
                        // Import resources for all alternate languages supported by the website.
                        IEnumerable<CultureInfo> cultures = web.SupportedUICultures;
                        foreach (CultureInfo culture in cultures)
                        {
                            // Skip the default language.
                            if (culture == web.UICulture)
                                continue;

                            // Try to get a resource file for the current language.
                            FileInfo fi = GetResxFileInfo(Environment.CurrentDirectory, culture.Name);
                            if (fi == null)
                                continue;

                            // If a file exists, import it.
                            using (FileStream fs = fi.OpenRead())
                            {
                                Console.WriteLine("Importing from {0}", fi.Name);
                                web.ImportUserResources(culture, fs);
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.Read();
        }

        static FileInfo GetResxFileInfo(string path, string cultureName)
        {
            DirectoryInfo di = new DirectoryInfo(path);

            string searchPattern = String.Format("*.{0}.resx", cultureName);

            FileInfo[] fi = di.GetFiles(searchPattern);

            return fi.Length > 0 ? fi[0] : null;
        }
    }
}
Show:
© 2014 Microsoft