请参见下面的函数,它将完全执行我上面提到的操作。
Private Sub LocalizeReport()
Dim xmlDoc As XmlDocument = New XmlDocument
Dim asm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
'create in memory, a XML file from a embedded resource
Dim xmlStream As Stream = asm.GetManifestResourceStream(ReportViewer1.LocalReport.ReportEmbeddedResource)
Try
'Load the RDLC file into a XML doc
xmlDoc.Load(xmlStream)
Catch e As Exception
'HANDLE YOUR ERROR HERE
End Try
'Create an XmlNamespaceManager to resolve the default namespace
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("nm", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")
nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")
'IMPORTANT LINE BELOW
'YOU WILL NEED TO SET THIS TO YOUR RESOURCE MANAGER, OTHERWISE NOTHING WILL WORK
Dim rm As ResourceManager = New ResourceManager("Insurance.Subs.WinUI.Controls.Resources", asm)
'Loop through each node in the XML file, that has the ValueLOCId property set.
'Using this property as a workaround for localization support. The value specified in this
'property will determine what resource to use for translation.
Dim node As XmlNode
For Each node In xmlDoc.DocumentElement.SelectNodes(String.Format("//nm:{0}[@rd:LocID]", "Value"), nsmgr) 'XPath to LocID
Dim nodeValue As String = node.InnerText
If (String.IsNullOrEmpty(nodeValue) Or Not nodeValue.StartsWith("=")) Then
Try
Dim localizedValue As String = node.Attributes("rd:LocID").Value
'Get the resource via string
localizedValue = rm.GetString(localizedValue)
If Not String.IsNullOrEmpty(localizedValue) Then
'Set the text value - via the retrieved information from resource file
node.InnerText = localizedValue
End If
Catch ex As Exception
'handle error
End Try
End If
Next
ReportViewer1.LocalReport.ReportPath = String.Empty
ReportViewer1.LocalReport.ReportEmbeddedResource = Nothing
'Load the updated RDLC document into LocalReport object.
Dim rdlcOutputStream As StringReader = New StringReader(xmlDoc.DocumentElement.OuterXml)
Using rdlcOutputStream
ReportViewer1.LocalReport.LoadReportDefinition(rdlcOutputStream)
End Using
End Sub
http://support.microsoft.com/kb/920769
你需要创建一个程序集,包含资源和方法,以根据语言环境获取字符串。你可以在这里找到完整的教程:http://www.codeproject.com/Articles/294636/Localizing-SQL-Server-Reporting-Services-Reports
你可以添加一个包含你想要翻译的字符串资源的自定义程序集,并在报表中访问它们。使用这个数据集,我进行简单的查找来进行翻译。
=Lookup("WORDTOBETRANSLATED", Fields!Key.Value, Fields!Value.Value, "dsTranslation")
=Microsoft.VisualBasic.Interaction.iif(Lookup("WORDTOBETRANSLATED", Fields!Key.Value, Fields!Value.Value, "dsTranslation") = "", "WORDTOBETRANSLATED", Lookup("WORDTOBETRANSLATED", Fields!Key.Value, Fields!Value.Value, "dsTranslation"))
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Data.SqlClient;
namespace CmsReportLibrary
{
public class DictionaryLabel
{
DictionaryLabel()
{
}
public static string[] GetDictionary(int languageid)
{
System.Data.SqlClient.SqlClientPermission oPerm = new System.Data.SqlClient.SqlClientPermission(PermissionState.Unrestricted);
oPerm.Assert();
SqlConnection oConn = new SqlConnection();
oConn.ConnectionString = ConfigurationManager.ConnectionStrings["appconnectionstring"].ConnectionString;
//oConn.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;User Id=<>;Password=<>";
//oConn.Open();
//SqlCommand oCmd = new SqlCommand();
//oCmd.Connection = oConn;
//oCmd.CommandText = "..................";
// ....................
//oConn.Close();
return new string[] {...............};
//ex return new string[] { "Client||Klient", "Week||Woche", "Year||Jahr"};
}
}
}
步骤2:编译库并将其复制到报告服务的Bin文件夹中
例如:将库复制到C:\Program Files\Microsoft SQL Server\MSRS10_50.R2\Reporting Services\ReportServer\bin
步骤3:修改ReportServer文件夹中的rssrvpolicy.config文件(例如:C:\Program Files\Microsoft SQL Server\MSRS10_50.R2\Reporting Services\rssrvpolicy.config),找到"$CodeGen$",并添加以下代码以让SSRS知道新库的位置。
<CodeGroup
class="UnionCodeGroup"
version="1"
PermissionSetName="FullTrust"
Name="CoDeMagSample"
Description="CoDe Magazine Sample. ">
<IMembershipCondition
class="UrlMembershipCondition"
version="1"
Url="C:\Program Files\Microsoft SQL Server\MSRS10_50.R2\Reporting Services\ReportServer\bin\CmsReportLibrary.dll"
/>
</CodeGroup>
步骤4:在报告服务配置管理器中停止和启动报告服务
步骤5:将库应用到SSRS报告中 创建新报告或修改现有报告,该报告应具有languageid参数 通过以下方式为此报告设置库的引用: 右键单击报告,选择“报告属性” 单击“引用”选项卡 将库的引用粘贴到“添加或删除程序集”区域:
CmsReportLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
第六步:添加自定义代码以支持标签翻译public Shared Dim ListLabel as String()
函数 GetListLabel(languageid as Integer) ListLabel = CmsReportLibrary.DictionaryLabel.GetDictionary(Parameters!LanguageId.Value) end function 函数 Translate(input as String) as String
dim i as Integer
For i=0 to UBound(ListLabel,1)
if Instr(ListLabel(i), input) > 0 then
Translate = Replace(ListLabel(i), input + "||","")
exit function
end if
Next
'Not found, return any string you want
Translate = "not found"
end function
列表标签变量的表达式
=Code.GetListLabel(Parameters!LanguageId.Value)
步骤8:翻译报告中的标签
修改标签表达式,使用自定义代码中的Translate方法进行翻译
例如:
=Code.Translate("Client")
=Code.Translate("Week")
我同意Igoy的观点,最近我按照codeproject中列出的步骤进行了操作,但是我想补充一下,当添加新的CodeGroup时,需要遵循的步骤有些不足,如果你将新的CodeGroup放在未命名的UnioncodeGroup之后(它的Url="$CodeGen$/*"),那么你尝试访问自定义程序集的尝试将会失败。
经过大量的挖掘,我在其中一个msdn页面上找到了确认信息(请参见“扩展的CodeGroup元素的放置”部分)。他们的措辞是“建议”,但从我的测试来看,至少在直接在报告服务器上进行测试时,这是必需的: http://msdn.microsoft.com/en-us/library/ms152828.aspx
在.config文件中,此位置的xpath如下(在wix中很有用): //PolicyLevel/CodeGroup/CodeGroup[[]@class='FirstMatchCodeGroup'[]]/CodeGroup[[]@PermissionSetName='ReportLocalization'[]]