1

I'm using Google Closure Template in order to write my application's UI using JavaScript. Look at this question for the detailed reason of why I'm using Google Closure Template. I want it to be multilingual. I see that there is a --locales switch and also looked at the samples provided in the project here and here. In the README_FOR_EXAMPLES files it is written that

+ simple_generated_en.js, features_generated_en.js,
simple_generated_x-zz.js, features_generated_x-zz.js The JS files generated by SoyToJsSrcCompiler when it is executed on simple.soy and features.soy (locales are 'en' and 'x-zz' with the translated XLIFF files from shared examples directory 'examples' and with the above compile-time globals file). We need both simple.soy and features.soy because some of the templates in features.soy call the templates in simple.soy. Note: For an example Ant target (and command line args) that generates these files, please see target 'js-features-example' within the top-level 'build.xml'.

What I expected was that it would generate just one JavaScript code base which will use desired strings from the appropriate locale file based on an option provided at runtime before the template function is called. Is that possible with closure templates?

Community
  • 1
  • 1
IsmailS
  • 10,797
  • 21
  • 82
  • 134

1 Answers1

1

As far as I can see, you can use a dictionary-object as a parametr for your template.

/**
 * @param dict
 */
{template .example}
    <h1>{$dict.title}</h1>
    <div>{$dict.content}</div>
{/template}

This object can be generated on the server-side from your locale file and transfered to javascript via script tag.

Otherwise you can load different compiled template file to the client side according to the locale.

There's also i18n possibility, but it's kinda useless for your problem, imo.

Mironor
  • 1,157
  • 10
  • 25
  • Can you please link me to the documentation regarding the dictionary-object? – IsmailS Aug 11 '11 at 07:04
  • OK I got it. Plain JavaScript Object will do. Although the problem now is it HTML escapes it using `soy.$$escapeHtml(opt_data.title)` but for me there can be HTML bits in the strings. Any idea for that? – IsmailS Aug 11 '11 at 08:06
  • Al-right! I found that too. If I have `{$dict.content |noAutoescape}` in my template, then I can pass HTML in the `content` and it won't be escaped i.e. `<` won't convert to `<`. – IsmailS Aug 11 '11 at 08:13
  • Yes, dictionary-object = plain javascript object. Also, if you need something more powerfull than noAutoescape, you can write your own template function ( http://code.google.com/closure/templates/docs/plugins.html#functions ) – Mironor Aug 11 '11 at 11:05
  • Thanks @Mironor, right now noAutoescape will do and also that we are working in on Microsoft Technology stack and don't have much of java skills. I hope we don't need to create any plugin. Closure Templates I'm just using to easily and get JavaScript to draw my DOM from the HTML given by the designer. – IsmailS Aug 11 '11 at 12:18