Introduction
If you need to display an RDLC report in one of several different languages, you need to create a localized version of the report for each language because RDLC reports do not support localization at the moment. However, that is complex and time-consuming, so it would be useful to localize the RDC reports similar to the way .NET handles localization normally.
Solution
The reports that you create for ReportViewer controls (.RDLC files) are XML files containing the definition of the report, so the procedure of localization doesn't look too difficult:
- Load the RDLC file using the
Load
method of the XMLDocument
class.
- Go through the nodes of the XML document and localize the value of the
Value
property of the static elements of the report.
- Load the updated RDLC document into the
LocalReport
object using the LoadReportDefinition
method.
To specify a resource key used for localization of the text of static elements, we can use a property named ValueLocID
- the localization identifier associated with the Value
property. I have not found any information about this property in any of the MSDN documentations or the RDL specification, and looks that this property is not used by the ReportViewer
control in any way, so I hope that it is safe enough to use this property to specify a resource key for the localized text. Similarly, we can use the ToolTipLocID
and LabelLocID
properties for the localization of the ToolTip
and Label
properties.
Code
private void LocalizeReport(LocalReport report)
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(Server.MapPath(ResolveUrl(report.ReportPath)));
}
catch (XmlException)
{
return;
}
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("nm", "http://schemas.microsoft.com/" +
"sqlserver/reporting/2005/01/reportdefinition");
nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/" +
"SQLServer/reporting/reportdesigner");
string resourcePath = Path.Combine(ResolveUrl("."),
Path.GetFileName(report.ReportPath));
foreach (string nodeName in new String[] { "Value",
"ToolTip", "Label" })
{
foreach (XmlNode node in doc.DocumentElement.SelectNodes(
String.Format("//nm:{0}[@rd:LocID]", nodeName), nsmgr))
{
String nodeValue = node.InnerText;
if (String.IsNullOrEmpty(nodeValue) || !nodeValue.StartsWith("="))
{
try
{
String localizedValue = (string)HttpContext.GetLocalResourceObject(
resourcePath, node.Attributes["rd:LocID"].Value);
if (!String.IsNullOrEmpty(localizedValue))
{
node.InnerText = localizedValue;
}
}
catch (InvalidCastException)
{
}
}
}
}
report.ReportPath = String.Empty;
using (StringReader rdlcOutputStream = new StringReader(doc.DocumentElement.OuterXml))
{
report.LoadReportDefinition(rdlcOutputStream);
}
}
An example of using of LocalizeReport
function is shown below:
protected void Page_Load(object sender, EventArgs e)
{
ReportViewer reportViewer = new ReportViewer();
reportViewer.LocalReport.ReportPath = "App_Data/Reports/Report.rdlc";
LocalizeReport(reportViewer.LocalReport);
reportViewer.LocalReport.SetParameters(...);
reportViewer.LocalReport.DataSources.Add(...)
form1.Controls.Add(reportViewer);
}
Step-by-Step Instructions
- Create the App_LocalResources folder.
- Create a resx file named after your report for each language you want to support (e.g., for a report file named Report.rdlc, you would create a Report.rdlc.resx for the default language, and Report.rdlc.es.resx for Spanish). Always have a default language resx file.
- Add whatever strings you need to the resx file.
- In your RDLC, enter the string names from the resx file as the value of the property of
ValueLocID
(e.g., for the resource string Hello, set the value of property ValueLocID
of textboxHello
to Hello).
- Call the
LocalizeReport
function. Note that you must run the LocalizeReport
function before setting parameters or data sources to the report; otherwise, it will not work.
References
This article is partly based on the example created by Joe Jone.