Skip to content

Latest commit

 

History

History
executable file
·
148 lines (100 loc) · 5.08 KB

README.md

File metadata and controls

executable file
·
148 lines (100 loc) · 5.08 KB

react-intl-po

Extract POT from react-intl and convert back to json.

messages.json → POT → PO → translation.json

Travis Codecov Status npm package npm downloads license

Dependency Status devDependency Status peerDependency Status

Installation

$ npm install react-intl-po --save

Requirements and Workflow

  • react-intl
  • babel-plugin-react-intl

RIP Workflow

Usage

There are two sub-commands of react-intl-po or rip:

  1. json2pot: Convert the json files extracted from babel-plugin-react-intl into one .pot file.
  2. po2json: Convert translated .po files back to .json format.

json2pot

$ rip json2pot '_translations/src/**/*.json' \
    -o ./mcs-public.pot
Arguments Description
srcPatterns The pattern of .json files extracted from babel-plugin-react-intl
output (-o) The output pathname of .pot file to be translated
message-key (-k) [Optional] Translation message ID key (default key is defaultMessage)
message-value (-v) [Optional] Translation message value key (default is to leave msgstr empty)

po2json

Case 1: Output one file per locale if a directory is set

$ rip po2json './node_modules/mcs-translation/po/mcs-public*.po' \
     -m './_translations/src/**/*.json' \
     -o './translations'

Case 2: Output one merged file if a .json file is set

$ rip po2json './node_modules/mcs-translation/po/mcs-public*.po' \`
     -m './_translations/src/**/*.json' \
     -o './translations.json'
Arguments Description
srcPatterns The pattern of translated .po files
messagesPattern (-m) The pattern of .json files extracted from babel-plugin-react-intl
output (-o) The output pathname of a file / directory
message-key (-k) [Optional] Translation message key (default key is defaultMessage)

API

Property

Q&A

How to translate the same message into two different meanings?

Set the message-key (-k) to 'id' of message object from babel-plugin-react-intl (default key is 'defaultMessage'). (#41)

$ rip po2json './node_modules/mcs-translation/po/mcs-public*.po' \
     -m './_translations/src/**/*.json' \
     -o './translations' \
     -k 'id'

$ rip po2json './node_modules/mcs-translation/po/mcs-public*.po' \`
    -m './_translations/src/**/*.json' \
    -o './translations.json' \
    -k 'id'

How to generate *.po instead of *.pot

Set the message-value (-v) to 'defaultMessage' of message object from babel-plugin-react-intl. The default behaviour omits values to generate a template file instead of a specific locale.

$ rip json2pot '_translations/src/**/*.json' \
    -o './mcs-public.po' \
    -k 'id' \
    -v 'defaultMessage'

Test

$ npm run test:watch

CONTRIBUTING

  • ⇄ Pull requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests ($ npm test).

MIT: http://michaelhsu.mit-license.org