Friday, April 29, 2016

Automating maintenance of i18n files

Have you ever wished in Backbone or Angular or Handlebars you didn't have to change your template and then go and update the i18n file as well?

Try this Nodejs code. It picks up the strings from your template files and builds a new i18n file each time you build from them for you to use:

var newKeyFile='var __ = {';

filenames.forEach(function(filename) {
if(filename.indexOf(".svn") == -1){/*if it is not the subversion file, parse the contents*/
var content = fs.readFileSync('templates/' + filename, 'utf-8');
while(content.length&&content.indexOf('{{_')!=-1){ /*while engliish strings remain to be extracted*/
content = content.substring(content.indexOf('{{_')); /*trim content*/
var stringToAdd = content.substring(5,content.indexOf('"}}'));
content = content.substring(content.indexOf('"}}')+3);


/*write master english key file so we don't have to manually maintain it*/
var lang = fs.writeFileSync("lang/"+locale+"_buildtime.js",newKeyFile,"utf-8");

No comments:

Post a Comment