If you are generating sites with webgen, when you need to add i18n to them, you have to make copies of the pages for each language. I want it something more like gettext. So I end up writing a new tag in webgen to use gettext. It’s working for me, YMMV. I’ve used webgen 0.5.10.
Installation
- Install webgen (you already have it, don’t you?)
- Install gettext gem. “gem install gettext”
- Download the code for the tag and uncompress it in the ext/ directory for your site.
- Add a ‘require “ext/gettext/task‘ in Rakefile after the webgen require.
- Set a default language for the site. Edit config.yaml and set a value for ‘website.lang‘.
Using it
The code in ext/ adds a new tag (gettext) which you have to use to mark strings as translatable (think of it as gettext()/_() from the gettext package). These strings can also be used on the page titles (only on the title attribute (see the list of limitations)).
After marking the strings, you have to run rake updatepo. This task collects translatable strings and creates/updates po template file in po/messages.pot. All the .page / .template files are used to collect the strings. Now make a directory with the language code inside po/ for each translation and copy messages.pot inside the new directory as messages.po. For example, having translations for english, spanish and german you’ll end up with:
po/en/messages.po
po/es/messages.po
po/de/messages.po
po/messages.pot
After translating the strings, you run rake makemo which creates the .mo files used when generating the site. For more help on po/mo files read gettext documentation or ruby-gettext docs.
You can download an example site using the tag.
Limitations / gory details
- When you run webgen, the site is generated with the configured language for the site. That means that to create a site with all the translations available you have to run N times webgen, each one for a different translation.
- If you run webgen, change translations in the .po files and afterwards want to do a ‘rake makemo webgen‘ I think it won’t work as webgen doesn’t notice this change. You’ll have to do a ‘rake clobber_webgen webgen‘
- gettext parser: the way the localizable strings are collected to create/update the po files is using ruby-gettext. I had problems with the lexer on certain inputs (i.e. I had google analytics code in a .page which caused a “trailing `E’ in number”. The solution/patch was to put this code in another file not processed by the parser and use include_file in the page/template.
- Using the gettext tag on the title is a bit of a lie, although you may think you are using the tag, the title, as the page meta info, is read as a YAML block. Which causes the “{gettext: msg}” syntax to be converted to a Hash. This is later fixed with a bit of monkeypatching.


