共用方式為


SPWeb.ImportUserResources method

匯入使用者所指定語言的資源。

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

Syntax

'宣告
Public Sub ImportUserResources ( _
    language As CultureInfo, _
    inputFile As Stream _
)
'用途
Dim instance As SPWeb
Dim language As CultureInfo
Dim inputFile As Stream

instance.ImportUserResources(language, _
    inputFile)
public void ImportUserResources(
    CultureInfo language,
    Stream inputFile
)

參數

  • inputFile
    Type: System.IO.Stream

    以使用者的資源資料資料流。必須是資料。Resx 檔案格式。

Exceptions

Exception Condition
InvalidOperationException

沒有替代語言會在網站上啟用。

InvalidOperationException

指定的語言不是其中一個已安裝的語言。

InvalidOperationException

可能是由該網站不支援的語言,或是它是預設語言。您無法匯入網站的預設語言的資源。

InvalidOperationException

輸入的檔不能匯入,因為它的語言與指定的語言不相符。

備註

您可以呼叫ExportUserResources方法,為每個站台所支援的語言建立資源檔,然後再為檔案提供給可以翻譯字串,其所包含的任何人。這項工作完成時,您可以呼叫ImportUserResources方法,以匯入轉譯,讓它們可以多語系使用者介面。

您無法匯入網站的預設語言的資源。新的文字,使用者介面中的預設語言會建立,並且後續翻譯成其他語言。預設的語言文字作為資源值替代語言之前提供的轉譯。如需詳細資訊,請參閱OverwriteTranslationsOnChange屬性。

Examples

下列範例是主控台應用程式,看起來的文化特性資源檔,每個網站所支援的替代語言。如果應用程式找到的資源檔,是它匯入它。

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

請參閱

參照

SPWeb class

SPWeb members

Microsoft.SharePoint namespace

OverwriteTranslationsOnChange

ExportUserResources(CultureInfo, Boolean, Stream)

SPList.UserResources

SPWeb.UserResources