How to access localized resource inside script tags?



  • Hello,

    I am in the process of adding localizations in my template, so far i'm able to add the localized strings in the html template as follows:

    <h1>{{$localizedResource.report.title}}</h1>
    

    The code above works. However, in some cases I also need to set the values text from inside a script tag:

    <p id="test">localize me</p>
    
    <script>
      const title = {{$localizedResource.report.title}};
    
      document.getElementById('test').innerHTML = title;
    </script>
    

    I have tried several variations of the snippet above to try and access the localization values from inside a script tag without success. The specific case where I need to do this is when generating a chartjs object that needs localized labels for the axes.

    Does anyone know how I can access the localization data from inside a script tag in the template?

    Much appriciated!



  • You probably want to do this

      const title = '{{$localizedResource.report.title}}';
    

    The best practice is to run the template with html recipe and use standard browser F12 tools to inspect the output.
    You would see an error in the console and quickly find out what is missing.



  • that did the trick indeed, thanks for the fix and the advice as well jan!


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.