All Projects → i18next → I18next Gettext Converter

i18next / I18next Gettext Converter

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to I18next Gettext Converter

Mantle
Model framework for Cocoa and Cocoa Touch
Stars: ✭ 11,319 (+7446%)
Mutual labels:  json
Carrots Admin Ajax
基于AngularJS1.3.18 + BootStrap 3.3.7 + +jQuery3.2.1 的后台管理系统方案
Stars: ✭ 147 (-2%)
Mutual labels:  json
Laravel Translatable String Exporter
Translatable String Exporter for Laravel
Stars: ✭ 149 (-0.67%)
Mutual labels:  json
Grafanajsondatasource
Grafana datasource to load JSON data over your arbitrary HTTP backend
Stars: ✭ 146 (-2.67%)
Mutual labels:  json
Keycloak Config Cli
Import YAML/JSON-formatted configuration files into Keycloak - Configuration as Code for Keycloak.
Stars: ✭ 147 (-2%)
Mutual labels:  json
Omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
Stars: ✭ 148 (-1.33%)
Mutual labels:  json
Spotify Json
Fast and nice to use C++ JSON library.
Stars: ✭ 145 (-3.33%)
Mutual labels:  json
Json Splora
GUI for editing, visualizing, and manipulating JSON data
Stars: ✭ 1,818 (+1112%)
Mutual labels:  json
Ladybug
A powerful model framework for Swift 4
Stars: ✭ 147 (-2%)
Mutual labels:  json
Coloquent
Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord-like (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects.
Stars: ✭ 149 (-0.67%)
Mutual labels:  json
Daw json link
Static JSON parsing in C++
Stars: ✭ 146 (-2.67%)
Mutual labels:  json
Goconfig
goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.
Stars: ✭ 146 (-2.67%)
Mutual labels:  json
Jhop
🏎Create fake REST API in one sec.
Stars: ✭ 148 (-1.33%)
Mutual labels:  json
Terse Logback
Structured Logging, Tracing, and Observability with Logback
Stars: ✭ 146 (-2.67%)
Mutual labels:  json
Utilsdmo
jsonManager json解析 采用 fastjson TimeUtils 时间类 除了时间格式化,提供用于视频的时长,和语音的时长的时间格式化 YUtils 类例如 获取屏幕宽
Stars: ✭ 149 (-0.67%)
Mutual labels:  json
Co
Art of C++. Flag, logging, unit-test, json, go-style coroutine and more.
Stars: ✭ 2,264 (+1409.33%)
Mutual labels:  json
Node Jq
Node.js wrapper for jq
Stars: ✭ 147 (-2%)
Mutual labels:  json
Jose2go
Golang (GO) implementation of Javascript Object Signing and Encryption specification
Stars: ✭ 150 (+0%)
Mutual labels:  json
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-1.33%)
Mutual labels:  json
Json 2 Kotlin
Convert JSON to Kotlin Data Classes
Stars: ✭ 148 (-1.33%)
Mutual labels:  json

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], for i18next < 12.0.0 use [email protected]<8.

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 [locale] -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]/[locale]/translation.json.

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

i18next-conv -l [locale] -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]/[locale]/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 [locale] -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 locale and a callback as its arguments. For the full API of the gettext object, check node-gettext. The function can then add/edit/delete translations, invoking the callback with an error object and the translation table.

eg.

// Delete all keys which do not belong to the frontend
module.exports = function (gt, locale, callback) {
  const clientSideSource = '/frontend/';
  const domain = 'messages';
  const translations = gt.catalogs[locale][domain].translations;
  gt.setLocale(locale); // Needed for when getComment is called

  Object.keys(translations).forEach(ctxt => {
    Object.keys(translations[ctxt]).forEach(key => {
      const comment = gt.getComment('messages', ctxt, key);
      if (comment) {
        if (comment.reference && comment.reference.indexOf(clientSideSource) === -1) {
          delete translations[ctxt][key];
        }
      }
    });
  });

  callback(null, translations);
};

Options

program
.version(i18nextConv.version)
.option('-b, --base [path]', 'Specify path for the base language file. only take effect with -K option', '')
.option('-f, --filter <path>', 'Specify path to gettext filter')
.option('-l, --language <locale>', '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('-k, --keyseparator [path]', 'Specify keyseparator you want to use, defaults to ##', '##')
.option('-P, --plurals <path>', 'Specify path to plural forms definitions')
.option('--project <project>', 'Specify the project-id-version when converting json to gettext')
.option('--quiet', 'Silence output', false)
.option('--gettextDefaultCharset', 'Default charset when parsing gettext files with gettext-parser', 'UTF-8')
.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')
.option('--foldLength', 'Specify the character fold length for strings', 76)
.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,
} = require('i18next-conv');

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

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].