Skip to main content

Localize texts in a report

jsreport (3.X versions) supports report generation in multiple languages. This how-to deals with the generation of a report in three languages (as an example): English, Finnish and Swedish.

Setting up translations

Add_asset_to_folder.png

The language file is a JSON data file, and it must contain a proper JSON structure (https://www.w3resource.com/JSON/structures.php). All language data files must contain the same structure. Only the values are different and translated to their particular language.

In the example below, you have a dataset in three languages. It contains basic information, such as language name, language code, and page title. Other than that, there are a set of field names that can be used as headers in a table to display portfolio information.

en.json

{
  "lang": "English",
  "langCode": "en",
  "title": "Report",
  "portfolioFields.id": "ID",
  "portfolioFields.shortName": "Short Name",
  "portfolioFields.name": "Name",
  "portfolioFields.currency": "Currency",
  "portfolioFields.country": "Country"
}

fi.json

{
  "lang": "Suomi",
  "langCode": "fi",
  "title": "Salkut",
  "portfolioFields.id": "ID",
  "portfolioFields.shortName": "Lyhyt nimi",
  "portfolioFields.name": "Nimi",
  "portfolioFields.currency": "Valuutta",
  "portfolioFields.country": "Maa"
}

sv.json

{
  "lang": "Sverige",
  "langCode": "sv",
  "title": "Portföljer",
  "portfolioFields.id": "ID",
  "portfolioFields.shortName": "Kort Namm",
  "portfolioFields.name": "Namm",
  "portfolioFields.currency": "Valuta",
  "portfolioFields.country": "Land"
}

Once these files are created, you can use them in you templates. There must be only one file per language.

Using translations in a report

To use the translations in a report, use the localize method and specify, as input parameters, the translation key and the relative path to the translation files:

{{localize "translationKey" "path"}}

For example, with the translations defined above, you will need to add this in your report if you want to translate the title:

{{localize "title" "/Portfolios"}}

jsreport searches for the translation files in the specified path and for a matching key within the correct language file. After that, jsreport fills in the corresponding value.

Testing the translations

To test the localization in the jsreport UI, do the following:

  1. Type the language code in the Template language field in the localization section in the Properties panel.

  2. Click Run to run the template: the report should then be localized in the language you selected in the previous step.

For example, if you want to test your report with the Finnish localization in the jsreport UI, you need to add fi in the Template language field.

When you run the report in the jsreport UI, it will use the values from the fi.json file.

Note that when generating your report from, for example, FA Back, the language you choose overrides the value you set in the Template language field.

Set_language_in_report.png