简体   繁体   中英

Google Closure Templates generates multiple JavaScript files for each language instead of single JavaScript code base with separate resource files

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM