New to Telerik Reporting? Download free 30-day trial

Localization of reports based on report parameter and RESX files

Environment

Product Version 12.1 18.516
Product Progress® Telerik® Reporting
Project Type MVC
Viewer HTML5 MVC Viewer
Visual Studio version Visual Studio 2017

Description

The following article explains how to create localizable reports according to report parameter containing the name of the culture for the specified localization resource. For example, to provide a localization resource for the German (de-De) culture, the corresponding resource file could be named: Resources.de-DE.resx.

Solution

Such application could be easily created following these steps:

  • Create new ReportLibrary project that would contain the report and it's corresponding resource files.
  • Configure new DataSource (for this example we are using CSVDataSourceWizard) which should contain two properties - Language (the names of languages) and Culture (their culture strings).
  • Create two new report parameters for the report. The first will be for the Language, so map the csvDataSource as AvaliableValues of the parameter. Let DisplayMemeber be = Fields.Language and ValueMember - = Fields.Culture. Finally, set it's Visibility to True. The second report parameter will give information about the base path the resources are searched at (check the report viewer's initialization file - Index.cshtml, that demonstrates how to get current base path for web applications).
  • Create Resource files with localized strings for each language according to report items values.
  • Create new user function that will get the localized value of the report item, based on the chosen parameter value. For example:
       public static string GetLocalizedText(string name, string cultureName, string basePath)
        {
            Dictionary<string, string> language;
            if (languages.TryGetValue(cultureName, out language))
            {
                return language[name];
            }

            var filePath = new Uri(new Uri(basePath), string.Format("bin\\Resources.{0}resx", cultureName)).AbsolutePath;

            filePath = Uri.UnescapeDataString(filePath);

            var reader = new ResXResourceReader(filePath);

            language = new Dictionary<string, string>();
            foreach (DictionaryEntry k in reader)
            {
                language.Add((string)k.Key, k.Value + string.Empty);
            }

            languages.GetOrAdd(cultureName, language);

            return language[name];
        }
  • The function is then used in expression to visualize text in the report.

See Also

You can download the sample MVC application illustrating the behavior from here.

In this article