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

纵然是瞬间 提交于 2019-12-08 04:15:56

问题


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.



来源:https://stackoverflow.com/questions/7008419/google-closure-templates-generates-multiple-javascript-files-for-each-language-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!