-->

Google Closure Templates generates multiple JavaSc

2019-06-01 18:33发布

问题:

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?

回答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.