angular-translate / grunt-po2json-angular-translate

grunt plugin to convert po to an angular-translate format
MIT License
8 stars 12 forks source link

grunt-po2json-angular-translate

grunt plugin to convert po to angangular-translate format

Getting Started

This plugin requires Grunt ~0.4.2

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-po2json-angular-translate --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-po2json-angular-translate');

The "po2json_angular_translate" task

Overview

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

grunt.initConfig({
  po2json_angular_translate: {
  options: {
     pretty: false,
     upperCaseId : false
    },
    your_target: {
                 files: {
                     'tmp/dest.json' : ['test/fixtures/*.po'], // This will generate a single json file with all the specified strings
                     'tmp/dest' : ['test/fixtures/*.po'] //this will create several json files with its own strings
                        }
    },
  },
});

Options

options.pretty

Type: Boolean Default value: false If you want to pretty print the result

options.upperCaseId

Type: Boolean Default value: false If you want to convert the ids to uppercase

options.cleanPrevStrings

Type: Boolean Default value: false It will remove all the previous generated files on the destination specified before creating the new ones.

options.enableAltPlaceholders

Type: Boolean Default value: true It enables you to use alternative placeholders format, it defaults with {foo}.

options.placeholderStructure

Type: Array Default value: ['{','}'] Here you can set your own placeholder structure. Notice that you must specify a closing mark.

Usage Examples

Default Options

grunt.initConfig({
  po2json_angular_translate: {
    options: {},
    files: {
      'dest/': ['src/**/*.po'],
    },
  },
});

Pluralization

To get the angular-translate format of pluralizations, we need to have a po file with the standard format, which would look something like:

msgid "button/save-change"
msgid_plural "button/save-changes"
msgstr[0] "Save %d Change"
msgstr[1] "Save %d Changes"

Which will be converted into:

  {"button/save-change": "Save {PLURALIZE, plural, offset:1 =2{# Change} other{# Changes}}"}

Please, for complex language pluralizations, like in German, I would recommend to make a quick scann, to make sure that everything makes sense.

Be sure that both (singular and plural) contain the same length, otherwise, the output won't be the desired one

Not

msgid "button/save-change"
msgid_plural "button/save-changes"
msgstr[0] "Save %d Change"
msgstr[1] "Save Changes"

Note that the placeholder for numbers should be: %d, so the plugin understands that there goes a number pluralization.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

0.0.1 First basic functionality. Just convert the po files into JSON objects compatible with angular-translate

Features planned

Changelog

v 0.0.3