Introduction
The metadata of an RDLC report is defined using XML. This property makes it easier for changing the definition of an RDLC report at runtime. Currently, many developers are facing a problem in localizing RDLC reports into different languages. The most common approach used is to build a different report for each language. In this article, we will present an easy to use approach to localize reports in different locales. And, since Arabic language (same as Hebrew) uses the Right-To-Left direction property, my example will be based on the Arabic Locale to provide the most comprehensive example. I will use Windows Reporting in this example. I will be using .NET 2.0 and Visual Studio 2005.
The Linguist
The linguist is the class that will replace the words in the report with the words from the correct locale. The words and their language replacements will be placed in an XML file. Each XML file represents a language. (We could have used the Resources method of .NET which is actually equivalent to this method, because the resource files are after all XML files; but, this is also another solution to look at.)
You need to place the text that you want to replace in the report in the LocID
property of the RDLC label, textbox, or any other object. Then, a loop will pass by all the objects, replacing the work or phrase in LocID
property with the correct word or phrase, and setting it to the Text
or Value
property of the object.
string languageId = Context.Current.CurrentLanguage.Id;
XmlDocument xmldoc = new XmlDocument();
if (languageCache.ContainsKey(languageId))
{
xmldoc = languageCache[languageId];
}
else
{
string xmlFilePath = System.Windows.Forms.Application.StartupPath +
"\\Localization\\Languages\\" + languageId + ".xml";
if (System.IO.File.Exists(xmlFilePath))
{
xmldoc.Load(xmlFilePath);
languageCache.Add(languageId, xmldoc);
}
else
{
return keyName;
}
}
StringBuilder xpathExpression = new StringBuilder();
xpathExpression.Append("/phrases/phrase[@id='");
xpathExpression.Append(keyName);
xpathExpression.Append("']");
XmlNode node = xmldoc.SelectSingleNode(xpathExpression.ToString());
if (node != null)
value = node.InnerText;
if (value == "") value = keyName;
To accomplish this, we need to set the tables and the other report controls used to start from the right – to do this, we must:
- Set the columns to start in the reverse order – meaning, let the first column be the last.
- Change the text align property: the right - make it left, and the left - make it right.
- Change the headers and footers of the tables.
- Work on the groups, and also reverse the alignment property and the column definitions.
XmlNode alignNode = null;
XmlNodeList textAlignNodes = doc.SelectNodes("//nm:TextAlign", nsmgr);
foreach (XmlNode node in textAlignNodes)
{
if (node.InnerText == "Right")
node.InnerText = "Left";
else if (node.InnerText == "Left")
node.InnerText = "Right";
alignNode = node;
}
XmlNodeList styleNodes = doc.SelectNodes("//nm:Textbox/nm:Style", nsmgr);
foreach (XmlNode node in styleNodes)
{
if (node.SelectSingleNode("./nm:TextAlign", nsmgr) == null)
{
if (alignNode != null)
{
XmlNode nodeForAlign = alignNode.Clone();
nodeForAlign.RemoveAll();
nodeForAlign.InnerText = "Right";
node.AppendChild(nodeForAlign);
}
}
}
ReorderNodes(
"/nm:Report/nm:Body/nm:ReportItems/nm:Table/nm:" +
"Details/nm:TableRows/nm:TableRow/nm:TableCells",
"nm:TableCell", doc, nsmgr);
ReorderMultipleGroupNodes(doc, nsmgr);
ReorderGroupNodes(
"/nm:Report/nm:Body/nm:ReportItems/nm:" +
"Table/nm:Footer/nm:TableRows/nm:TableRow",
"./nm:TableCells", "./nm:TableCells/nm:TableCell", doc, nsmgr);
ReorderGroupNodes(
"/nm:Report/nm:Body/nm:ReportItems/nm:Table/" +
"nm:Header/nm:TableRows/nm:TableRow",
"./nm:TableCells",
"./nm:TableCells/nm:TableCell", doc, nsmgr);
ReorderNodes("/nm:Report/nm:Body/" +
"nm:ReportItems/nm:Table/nm:TableColumns",
"nm:TableColumn", doc, nsmgr);
Points of Interest
This code can more developed to fit other objects and controls of the report such as the List
control and also the floating objects - other than this, this code is very much efficient in Arab-izing tables however complex they are (heavy grouping, use of subreports, ...). To complete this development, we can also replace the toolbar with a custom toolbar with Arabic labels and tooltips - this was done and published in more than one article in The Code Project.