Using gulp to compile .po files to json for e.g. AngularJS $translate

If you need to deal with multiple languages in the web, there is no way around gettext. you can recognise a project using gettext, if its locale files consist of .po files and most of the time one .pot (gettext template file) which is the basefile for all language keys and the origin translation. .po fines are fine to handle as most translation agencies are able to work with them as they are easy to handle.

But if you want to use gettext files in your javascript web project, there are further steps needed to use them. The best way is to include it in your build process and which lets it run automatically upon change and get a json file out of it, which can be consumed with AngularJS $translate’s static file loader.

In my case this is gulp, the famous taskrunner. but also grunt has the same functionality (also via plugin) in place.
You should have node and gulp already set up for your project, we’re starting from there on.

Installing dependencies and configuring gulpfile.js to compile po files

Create your .po locale files

I have used PoEdit to create my files, saving them in a subdirectory ´src/locales/locale-en.po´.
You might also use this sample data for your first steps:

Install dependencies:

Create task in gulpfile.js:

In my case the data coming our of gulp-po-json didn’t fit directly to be used for AngularJS, thats why I need jsonEditor to transform the converted data.
This is because I am grouping my locales as you can see in the sample – using a describer, a dot and the key itself ( AUTH.SIGN_IN, COMMON.OK ). I think this is a good practise to easily find module related locales.

If you run this task with ´gulp translate´, the outcome should look like that:

Pretty nice, huh 🙂

Until now, this can used with any framework out there (but using java, php, ruby, I would rather go and work directly with the po, since there are classes for that).

AngularJS + static locale json

If you haven’t used $translate yet, you might have a look at bower, or use the cdn.
Bower:

Using it with pascalprecht.translate

You are ready to use them now in your AngularJS app. More about usage of pascalprecht’s angular-translate.

markus