3

I have a project where I use require.js and hbs.js (require-handlebars-plugin), among others. Everything is working fine in my development environment, but after I optimized the project using r.js (requirejs optimizing), I have a problem.

That is; because hbs.js precompiles only one locale the whole i18n logic is broken, it is no longer possible to change locale. I suspect that I somehow have to do the optimizing process for each locale, but I can't see how. It would be really nice if someone can share their solution to this problem

Thanks!

1 Answers1

1

Ok, here is how I solved it:

First I created one require.js bootstrap file for each language. Then I modified my maven pom.xml to perform the javascript optimization twice, one for each locale. I used requirejs-maven-plugin with two execution elements, one for each locale. Each of the elements have a configFile value which points to the respective r.js build files. (One for each language). The build file for a locale then points to the bootstrap file for that language.

After running the optimization you will now have a uglified js-file of each locale. But chances are that none of these are included in the final war file. You have to copy the uglified files from the r.js build_out folders into the war. I did this by adding configuration elements to the maven-war-plugin; warSourceDirectory and webResources to copy my two locales. I also used webSourceInclude and webSourceExlude to remove the files I did not want.

The last step was to load the correct require bootstrap file when loading the site. To do this simply create a javascript snippet in your index.html which first determines your locale (from a cookie for example), and then dynamically load the bootstrap file

document.write("<script data-main='bootstrap_" + getLocaleString() + "' src='/js/lib/require.js'><\/script>");

Thanks to the author of https://github.com/SlexAxton/require-handlebars-plugin for helping me figure this out.