New to Telerik Reporting? Download free 30-day trial

How to Localize Report Server-Side Based on Report Parameter Value

Environment

Product Progress® Telerik® Reporting

Description

In my scenario, the users would select the Report culture as a value of a Report Parameter in the Html5 Report Viewer. How can I apply the selected culture server-side for the Report rendering?

Solution

The CreateInstance and CreateDocument virtual methods are called always when you need to render the report in a viewer that utilizes the Telerik Reporting REST Service for generating reports. In the former you have the client reportSource, hence you may take the selected culture from the corresponding parameter. In the latter, you may provide the culture to the Reporting engine through the DeviceInfo dictionary in the CreateDocumentArgs args argument.

Here is sample code, which assumes that the Report has a parameter called lang that holds the selected culture name:

[Route("api/reports")]
public class ReportsController : ReportsControllerBase
{
    public static string Lang { get; set; }

    public ReportsController(IReportServiceConfiguration reportServiceConfiguration)
        : base(reportServiceConfiguration)
    {
    }

    //...

    public override IActionResult CreateInstance(string clientID, [FromBody] ClientReportSource reportSource)
    {
        Lang = reportSource.ParameterValues["lang"].ToString();

        return base.CreateInstance(clientID, reportSource);
    }

    public override IActionResult CreateDocument(string clientID, string instanceID, [FromBody] CreateDocumentArgs args)
    {
        args.DeviceInfo["CurrentCulture"] = Lang;
        args.DeviceInfo["CurrentUICulture"] = Lang;

        return base.CreateDocument(clientID, instanceID, args);
    }
}

The idea is to introduce a static string property, set its value in the CreateInstance method and use this value in the CreateDocument method.

The ReportsController gets instantiated on each request. Therefore, if the variable holding the culture name is not static, the value will be lost between the requests.

If you need to localize also the Text specifying the names of the Report Parameter editors in the Html5 Viewers, you need to use the renderingEnd event to select the parameter editor's element and change its text based on the selected culture. Here is a sample jQuery code for the event handler:

renderingEnd: function (e, args) {
    var langValue = e.data.sender.reportSource().parameters["lang"];

    var langParam = $(`.trv-parameter-header div[title='Language']`);
    if (langValue == "bg-BG") {
        langParam.text("Език");
    } else {
        langParam.text("Language");
    }
}

The reason for this is that the Parameters area of the viewer gets rendered with viewer loading, after calling the Get Report Parameters request that returns the visible Report Parameters with their default values. When the user selects new parameter values in the viewer, the Get Report Parameters request is not performed and the names of the editors are not updated.

See Also

In this article