node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org »

hernanex3-grunt-static-i18n

Static Internationalization

Build Status Coverage Status

Grunt plugin to translate static assets.

Say you have:

app/
├── Gruntfile.js
└── app
    ├── locale
    │   ├── fr
    │   │   └── LC_MESSAGES
    │   │       └── messages.po
    │   └── pt_BR
    │       └── LC_MESSAGES
    │           └── messages.po
    └── static
        └── data.json  // content: ["_('Hello World')"]

And you need to translate data.json.
Static internationalization would like like:

app/
├── Gruntfile.js
└── app
    ├── i18n
    │   ├── fr
    │   │   └── static
    │   │       └── data.json  // content: ["Bonjour tout le monde"]
    │   ├── pt_BR
    │   │   └── static
    │   │       └── data.json // content: ["Olá mundo"]
    │   └── static
    │       └── data.json  // not translated: ["Hello World"]
    ├── locale
    │   ├── fr
    │   │   └── LC_MESSAGES
    │   │       └── messages.po
    │   └── pt_BR
    │       └── LC_MESSAGES
    │           └── messages.po
    └── static
        └── data.json

Getting Started

This plugin requires Grunt.

Translations are done with node-gettext and you will need a proper gettext catalog (structure seen above). If need help extracting translation strings, checkout grunt-i18n-abide. Checkout the makemessages task in Gruntfile.js.

The "statici18n" task

Overview

In your project's Gruntfile, add a section named statici18n to the data object passed into grunt.initConfig():

grunt.initConfig({
  statici18n: {
    options: {
      localeDir: 'app/locale'
    },
    myAppTask: {
      files: [{
        expand: true,
        cwd: 'app',
        src: 'static/*.{js,json}',
        dest: 'app/i18n'
      }]
    }
  }
})

Options

options.localeDir

Type: String Default value: locale

Sometimes easiest to use a var, say <%= abideCreate.options.localeDir %>

options.template.interpolate

Type: RegEx
Default: search for _('msgid') or _("msgid")

Used to find gettext calls.
Sets _.templateSettings.interpolate

options.textDomain

Type: String Default value: messages

Name of your po files: locale/<lang>/LC_MESSAGES/<textDomain>.po

Tests

Run grunt && open coverage.html

License

MIT. Read the LICENSE file