共用方式為


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

Syntax

'Declaration
<SubsetCallableExcludeMemberAttribute(SubsetCallableExcludeMemberType.MarshalByRefObject)> _
Public Sub ImportUserResources ( _
    language As CultureInfo, _
    inputFile As Stream _
)
'Usage
Dim instance As SPWeb
Dim language As CultureInfo
Dim inputFile As Stream

instance.ImportUserResources(language, _
    inputFile)
[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.

Exceptions

Exception Condition
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.

Remarks

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.

Examples

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("https://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;
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.RootWeb

                If web.IsMultilingual Then

                    ' Import resources for all alternate languages supported by the website.
                    Dim cultures As IEnumerable(Of CultureInfo) = web.SupportedUICultures
                    For Each culture As CultureInfo In cultures

                        ' Skip the default language.
                        If culture Is web.UICulture Then
                            Continue For
                        End If

                        ' Try to get a resource file for the current language.
                        Dim fi As FileInfo = GetResxFileInfo(Environment.CurrentDirectory, culture.Name)
                        If fi Is Nothing Then
                            Continue For
                        End If

                        ' If a file exists, import it.
                        Using fs As FileStream = fi.OpenRead()
                            Console.WriteLine("Importing from {0}", fi.Name)
                            web.ImportUserResources(culture, fs)
                        End Using

                    Next

                End If

            End Using
        End Using
        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

    Function GetResxFileInfo(ByVal path As String, ByVal cultureName As String) As FileInfo
        Dim di As New DirectoryInfo(path)

        Dim searchPattern As String = String.Format("*.{0}.resx", cultureName)

        Dim fi As FileInfo() = di.GetFiles(searchPattern)

        Return If(fi.Length > 0, fi(0), Nothing)
    End Function

End Module

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

OverwriteTranslationsOnChange

ExportUserResources(CultureInfo, Boolean, Stream)

SPList.UserResources

SPWeb.UserResources