Click here to Skip to main content
Click here to Skip to main content

Localization of RDLC Reports into an Arabic Locale

, 10 Dec 2008
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Ali Hamdar
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

 
QuestionAnother way PinmemberMember 921835916-Aug-13 3:44 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:47 
Move strings from code to resx and translate their automatically. Try RGreatEx[^] free.
 
---
Best regards,
Alexander Nesterenko
Safe Develop Team
www.safedevelop.com

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by Ali Hamdar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid