Guess what? Last time we talked about localizing Report Viewer, one major thing was left out: why localize the viewer and not the report!
Obviously, if you’re about to send a report to someone who needs a translated viewer, you far-sightedly might want the report itself to be translated, too. Because even though in certain cases numbers can speak for themselves, a bit of help never hurts
Good news is that with Report Sharp-Shooter, all table headers, messages, and other static text can be translated on the fly – in exactly the same way as it works for Report Viewer. In this post, we’ll examine this technique and build a report displaying a message in Latin.
How to localize a custom message.
First thing to remember, is that SharpShooter localizations are normally stored in “%ProgramFiles%\Perpetuum Software\Net ModelKit Suite\Localization folder, where each language is represented by a separate .xml file. A new translation for existing language means just one more entry in the corresponding file, and if we want to support a totally new language, we need a new file for that.
In our case, we want both. In English.xml we would add
<Section ident="ReportLocalization.Samples"> <String ident="CodingWisdom">Code behaves like its developer</String> </Section>
..and also create Latin.xml with a translation (done by your humble correspondent using Google translate and a bit of guessing):
<?xml version="1.0" encoding="utf-16" standalone="yes"?> <Localization language="Latin" culture="LA" description="Latin"> <Section ident="ReportLocalization.Samples"> <String ident="CodingWisdom">Codus developerus simile est</String> </Section> </Localization>
That’s it. We take Localization project we created on BitBucket last time, open up the report in Report Designer, and drag-n-drop a TextBox there.
Using “Language” class.
And now the magic. As we know, report localization happens in Language class of SharpShooter – more precisely, in its GetString method. This guy returns translated messages based on the currently selected language. So as we are in Report Designer, we go to the Value binding of the newly created textbox, and write the following line:
PerpetuumSoft.Framework.Localization .Language.CurrentLanguage .GetString("ReportLocalization.Samples", "CodingWisdom", "<not found>");
The first parameter defines the section name, the second one points out the message key within the section, the third parameter is the default value in case the translation is not found (normally you’d probably want English version there).
Once that is done, we can enjoy the English version:
..and after changing the current language to Latin – either via our custom dropdown or using the built-in “Select Language” dialog – the Latin translation comes out in its supreme plenitude:
Summary.
As we have seen, not only it’s possible to localize Report Viewer (ie, default content like buttons, tooltips etc) but also report itself is localizable so that people could see its messages, table headers etc in their favorite language.
To get that working, one should put the translations to the necessary localization files of SharpShooter, and then setup Value binding of the corresponding textboxes in the report.
As usual, you’re welcome to browse source code of this example here at BitBucket, contribute any fixes and improvements, or just download a zip archive and play around.
Happy reporting!