Skip to content

converts gettext .mo or .po to 18next json format and vice versa

License

Notifications You must be signed in to change notification settings

rijk/i18next-gettext-converter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

i18next-gettext-converter

Travis npm package Coverage Status Dependency Status devDependency Status

Introduction

Project goal is to convert files from gettext to i18next json format and vice versa.

Installation

  1. first install node.js from nodejs.org.
  2. npm install i18next-conv -g

For i18next < 2.0.0 use [email protected], for i18next < 3.0.0 use [email protected]

Usage

convert .mo or .po to i18next json

in your console type:

for help:

i18next-conv -h

to convert a .mo or .po file to i8next json:

i18next-conv -l [domain] -s [sourcePath] -t [targetPath]

eg.: i18next-conv -l en -s ./locales/en.po -t ./locales/en/translation.json

if no target (-t) is specified file will be stored to [sourceDir]/[domain]/translation.json.

to convert i18next json to a .mo or .po file:

i18next-conv -l [domain] -s [sourcePath] -t [targetPath]

eg.: i18next-conv -l en -s ./locales/en.translation.json -t ./locales/en/translation.mo (or .po)

if no target (-t) is specified file will be stored to [sourceDir]/[domain]/translation.po.

for utf8-encoded po-files add these lines to your po file:

"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

It is necessary if you get corrupted output from the command above.

to filter incoming po-file translations, pass the path to a module that exports a filter function:

i18next-conv -l [domain] -s [sourcePath] -t [targetPath] -f [filterPath]

eg.: i18next-conv -l en -s ./locales/en.po -t ./locales/en/translation.json -f ./filter.js

The filter module should export a single function that accepts the gettext object, the domain and a callback as its arguments. The function can then add/edit/delete translations, invoking the callback with an error object and the translation table.

eg.

module.exports = function(gt, domain, callback) {
  var err;

  // Delete all keys without comments
  gt.listKeys(domain).forEach(key => {
    var comment = gt.getComment(domain, "", key);
    if (!comment) {
      gt.deleteTranslation(domain, "", key);
    }
  });

  callback(err, gt._domains[gt._textdomain]._translationTable);
};

Options

program
.version(i18nextConv.version)
.option('-b, --base [path]', 'Sepcify path for the base language file. only take effect with -K option', '')
.option('-f, --filter [path]', 'Specify path to gettext filter')
.option('-l, --language [domain]', 'Specify the language code, eg. \'en\'')
.option('-p, --pot', 'Generate POT file.')
.option('-s, --source [path]', 'Specify path to read from')
.option('-t, --target [path]', 'Specify path to write to', '')
.option('-K, --keyasareference', 'Deal with the reference comment as a key', false)
.option('-ks, --keyseparator [path]', 'Specify keyseparator you want to use, defaults to ##', '##')
.option('-P, --plurals [path]', 'Specify path to plural forms definitions')
.option('--quiet', 'Silence output', false)
.option('--skipUntranslated', 'Skip untranslated keys when converting into json', false)
.option('--splitNewLine', 'Silence output', false)
.option('--ctxSeparator [sep]', 'Specify the context separator', '_')
.option('--ignorePlurals', 'Do not process the plurals')
.parse(process.argv);

API

This module exposes a few functions to convert json to gettext and gettext to json. It accepts the same options as the cli.

const path = require('path');
const { readFileSync, writeFileSync } = require('fs');
const {
  i18nextToPo,
  i18nextToPot,
  i18nextToMo,
  gettextToI18next,
};

const source = path.join(__dirname, '../locales/ua-UK/translation.json');
const options = {/* you options here */}

function save(target) {
  return result => {
    writeFileSync(result, target);
  };
}

i18nextToPo('ua-UK', readFileSync(source), options).then(save('../locales/ua-UK/translation.po'));
i18nextToPot('ua-UK', readFileSync(source), options).then(save('../locales/ua-UK/translation.pot'));
i18nextToMo('ua-UK', readFileSync(source), options).then(save('../locales/ua-UK/translation.mo'));

gettextToI18next('ua-UK', readFileSync('../locales/ua-UK/translation.po'), options)
.then(save('../locales/ua-UK/translation.json'));

All credits go to

License

See the LICENSE file for license rights and limitations.

About

converts gettext .mo or .po to 18next json format and vice versa

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%