SPWeb.ExportUserResources method

Exports user resources for a given language.

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

'Declaration
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)

Parameters

language
Type: System.Globalization.CultureInfo

Specifies the language of the resources to be exported. The language must be listed in the collection returned by the SupportedUICultures property.

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.

ExceptionCondition
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.

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.

Caution noteCaution

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.

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

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
Show: