cancel
Showing results for 
Search instead for 
Did you mean: 

howto use localization/msg in .processTemplate

scapeler
Champ in-the-making
Champ in-the-making
How to process a freemarker template and also make use of a corresponding localization file (.properties).

This javascript returns error: Expression msg is undefined
var template = "${msg('at')}";
var result = node.processTemplate(template);
3 REPLIES 3

ddraper
World-Class Innovator
World-Class Innovator
Hi,

Could you provide a bit more context please on what you're attempting to do?  Is this a FreeMarker template that's part of a WebScript? Are you trying to customize and existing WebScript or create your own? I'm assuming that the code fragment you've provided is within a <script> element in a FreeMarker template and that this isn't code in the JavaScript controller for the WebScript? Are you doing this as part of an Alfresco project, etc - e.g. this isn't just a plain FreeMarker template?  If you could provide this information I should hopefully be able to help resolve your problem,

Regards,
Dave

scapeler
Champ in-the-making
Champ in-the-making
I want to (re)build parts of a page in share. Use YUI panels and fill those dynamically with template and data depending on the functionality and situation. Retrieve template(s) from the repository, name selection with "screencode", and replace labels/text with i18n translation text.

something like:
url: /api/ui?screencode=E00001&i18n=nl_NL  or /api/ui?screencode=E00002&i18n=nl_NL  etc.
template with names like:
abc-ui-E00001.ftl
abc-ui-E00002.ftl
i18n files with names like:
abc-ui-E00001_nl_NL.properties
abc-ui-E00002_nl_NL.properties

I can read a template from the repository (contentnode) in data webscript.
I can read I18n file from repository (contentnode) in the same data webscript..
Optional I can read data from repository.
I can call node.processTemplate(template); optional with args.

But how to make the translations for labels work.
Standard msg() does not work for me, "Expression msg is undefined" ${msg('at')}.

I think my next step is to place the i18n content into the model like model.labels.label.name and use ${labels.label.name} instead in the template. But maybe there is a more easy or standard method like using msg().

I use my own webscripts. Given code (as part of Javascript controller) was example of processTemplate with msg() function in the template (in this case only as string). It is for my own project.

I hope this is clear enough.

scapeler
Champ in-the-making
Champ in-the-making
This is how I solved my problem.

arguments in url: "?uinode=ui-E00001&htmlid=00001&i18n=nl_NL"

webscript ui.get.js to retrieve template and i18n from repository and processTemplate.
function main()
{
  arguments["htmlid"]=args["htmlid"];
  arguments["uinode"]=args["uinode"];

  // retrieve template from repository
  var luceneQueryftl = "@cm\\:name:\"" + args["uinode"] + ".ftl\"";
  resultftl = search.luceneSearch(luceneQueryftl);
  var template = resultftl[0];
  if (template == null) {
    model.uiout = "Template " + args["uinode"] + ".ftl not found!"
    return;
  }

  // retrieve i18n properties from repository (json formatted)
  var luceneQueryi18n = "@cm\\:name:\"" + args["uinode"] + "_" + args["i18n"] + ".properties\"";
  resulti18n = search.luceneSearch(luceneQueryi18n);
  if (resulti18n[0] != null) {
    var properties = jsonUtils.toObject(resulti18n[0].content);
    // put i18n tags in arguments for template processing
    for (var key in properties) {
      arguments["i18n_"+key]=properties[key];
    }
  }

  result = template.processTemplate(template.content, arguments);
  model.uiout = result;
}

main();

template (ui.get.html.ftl) for webscript only contains: ${uiout}

Template ui-E00001.ftl in repository:
<fieldset class="ui-fieldset" id="${args.htmlid}-fieldset-ui">
         <div class="hd" style="cursor: move;" id="${args.htmlid}-ui-hd"><div class="tl"></div><span>${args.i18n_type!"Type"}</span><div class="tr"></div></div>
        <div class="bd">
         <div class="yui-gd">
            <table>
            <tr>
              <td class="label-td-left">${args.i18n_name!"Name"}: </td>
              <td>
               <input class="text name" type="text" id="${args.htmlid}-name" name="name" size="80" value=""/>
              </td>
            </tr>
            </table>
           </div>
         </div>
        <div class="ft"><div class="bl"></div><span></span><div class="br"></div></div>
      </fieldset>

i18n file ui-E00001_nl_NL.properties in repository:
{
"name":"Naam",
"type":"Type"
}