Skip to content

Instantly share code, notes, and snippets.

@miguelgr
Forked from dadon/handlebars_translate.js
Created April 17, 2014 18:22
Show Gist options
  • Save miguelgr/11002818 to your computer and use it in GitHub Desktop.
Save miguelgr/11002818 to your computer and use it in GitHub Desktop.
Handlebars.registerHelper('trans', function(fn) {
return gettext(fn(this));
});
import optparse
import re
p = optparse.OptionParser()
p.add_option('--infile')
p.add_option('--outfile', default='translate.js')
options, arguments = p.parse_args()
f = open(options.infile, 'r')
messages = re.findall(r'{{#trans}}(.*?){{/trans}}', ''.join(f.readlines()))
messages_js = ''.join(['gettext(\'%s\');\n' % msg for msg in messages])
f = open(options.outfile, 'a')
f.write(messages_js)
f.close()
find ./ -iname "*.html" -exec python parse_handlebars_translate.py --infile={} \;
<div>
{{#trans}}Translate 1{{/trans}}
<p>{{#trans}}Translate 2{{/trans}}</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment