Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#
Article

Localization of RDLC Reports into an Arabic Locale

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
10 Dec 2008CPOL2 min read 45.8K   941   12   3
Localize RDLC into Arabic Locale or any other language with the correct right-to-left alignment.

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.

C#
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:

  1. Set the columns to start in the reverse order – meaning, let the first column be the last.
  2. Change the text align property: the right - make it left, and the left - make it right.
  3. Change the headers and footers of the tables.
  4. Work on the groups, and also reverse the alignment property and the column definitions.
C#
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);
      }
   }
}

//reorder details cells
ReorderNodes(
    "/nm:Report/nm:Body/nm:ReportItems/nm:Table/nm:" + 
    "Details/nm:TableRows/nm:TableRow/nm:TableCells",
    "nm:TableCell", doc, nsmgr);

//reorder table groupings cell
ReorderMultipleGroupNodes(doc, nsmgr);

//reorder footer cells
ReorderGroupNodes(
    "/nm:Report/nm:Body/nm:ReportItems/nm:" + 
    "Table/nm:Footer/nm:TableRows/nm:TableRow",
    "./nm:TableCells", "./nm:TableCells/nm:TableCell", doc, nsmgr);

//reorder header cells
ReorderGroupNodes(
    "/nm:Report/nm:Body/nm:ReportItems/nm:Table/" + 
    "nm:Header/nm:TableRows/nm:TableRow",
    "./nm:TableCells", 
    "./nm:TableCells/nm:TableCell", doc, nsmgr);

//reverse column definitions
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Integrated Digital Systems - IDS
Lebanon Lebanon
Been programming since 2001 interested in finance, security, workflows, SharePoint and algorithms. He is an MCSD, MCDBA, MCAD, MCSD, (again), MCTS, MCPD and MCT.
My Blog: www.alihamdar.com

Comments and Discussions

 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:47
Alexander Nesterenko17-Dec-08 21:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.