SPWeb.ExportUserResources Method

Exports user resources for a given 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 ExportUserResources ( _
    language As CultureInfo, _
    allResources As Boolean, _
    outputFile As Stream _
)
'Usage
Dim instance As SPWeb
Dim language As CultureInfo
Dim allResources As Boolean
Dim outputFile As Stream

instance.ExportUserResources(language, _
    allResources, outputFile)
[SubsetCallableExcludeMemberAttribute(SubsetCallableExcludeMemberType.MarshalByRefObject)]
public void ExportUserResources(
    CultureInfo language,
    bool allResources,
    Stream outputFile
)

Parameters

  • allResources
    Type: System.Boolean

    true to include all user-specified text, including text that has been translated; false to include only untranslated text.

  • outputFile
    Type: System.IO.Stream

    The destination for exported user resources.

Exceptions

Exception Condition
InvalidOperationException

No alternate languages are enabled on the site.

InvalidOperationException

The specified language is not one of the installed languages.

InvalidOperationException

The language is not supported by the website.

Remarks

The user resources are written to the specified stream in .Resx file format. For more information, see Resources in .Resx File Format. Text that has not been translated to the language specified by the language parameter is in the default language for the website. The default language for a site is returned by the UICulture property.

Warning

Code behind the Export Translations page in the user interface calls the ExportUserResources method. However, the logic implied by the allResources parameter is exactly the opposite of the logic implied by the user interface, which offers the option to Only export text that is not translated. If you choose Yes, then page code passes false in the allResources parameter. If you choose No, then page code passes true in the allResources parameter.

The user resources that this method exports include not only resources for the website but also resources for lists in the site. 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.

Examples

The following example is a console application that enumerates the languages supported by a website and exports untranslated resources for each language.

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)
                {
                    // Get the default language for the website.
                    int lcid = (int)web.Language;
                    CultureInfo defaultCulture = new CultureInfo(lcid);

                    // Export resources for all alternate languages supported by the website.
                    if (web.IsMultilingual)
                    {
                        string resxFileName;

                        IEnumerable<CultureInfo> cultures = web.SupportedUICultures;
                        foreach (CultureInfo culture in cultures)
                        {
                            if (culture.LCID == defaultCulture.LCID)
                                continue;
                            // Export only untranslated resources.
                            resxFileName = WriteResourceFile(web, culture.LCID, true);
                            Console.WriteLine("Resources for {0} exported to {1}.", culture.Name, resxFileName);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.Read();
        }

        static string WriteResourceFile(SPWeb web, int lcid, bool onlyUntranslated)
        {
            CultureInfo ci = new CultureInfo(lcid);

            string fileName = web.Title + "." + ci.Name + ".resx";
            char[] invalidChars = Path.GetInvalidFileNameChars();
            foreach (char c in invalidChars)
            {
                fileName.Replace(c, '_');
            }

            FileStream fs = File.Create(fileName);
            web.ExportUserResources(ci, !onlyUntranslated, fs);
            fs.Close();
            
            return fileName;
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports Microsoft.SharePoint

Module ConsoleApp

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

                ' Get the default language for the website
                Dim lcid As Integer = CInt(web.Language)
                Dim defaultCulture As New CultureInfo(lcid)

                ' Export resources for all alternate languages supported by the website.
                If web.IsMultilingual Then
                    Dim resxFileName As String

                    Dim cultures As IEnumerable(Of CultureInfo) = web.SupportedUICultures
                    For Each culture As CultureInfo In cultures
                        If culture.LCID = defaultCulture.LCID Then
                            Continue For
                        End If
                        ' Export only untranslated resources.
                        resxFileName = WriteResourceFile(web, culture.LCID, True)
                        Console.WriteLine("Resources for {0} exported to {1}.", culture.Name, resxFileName)
                    Next
                End If

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

    Function WriteResourceFile(ByVal web As SPWeb, ByVal lcid As Integer, ByVal onlyUntranslated As Boolean) As String
        Dim ci As New CultureInfo(lcid)

        Dim fileName As String = web.Title + "." + ci.Name + ".resx"
        Dim invalidChars As Char() = Path.GetInvalidFileNameChars()
        For Each c As Char In invalidChars
            fileName.Replace(c, "_"c)
        Next

        Dim fs As FileStream = File.Create(fileName)
        web.ExportUserResources(ci, Not onlyUntranslated, fs)
        fs.Close()

        Return fileName
    End Function

End Module

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

ImportUserResources(CultureInfo, Stream)

SPList.UserResources

SPWeb.UserResources