diff --git a/README.md b/README.md index 34acb69..2ff6114 100755 --- a/README.md +++ b/README.md @@ -58,11 +58,12 @@ $ 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 key (default key is `defaultMessage`) | +| **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 @@ -115,6 +116,17 @@ $ rip po2json './node_modules/mcs-translation/po/mcs-public*.po' \` -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 ``` diff --git a/src/cli.js b/src/cli.js index 1ecd467..ea244ab 100644 --- a/src/cli.js +++ b/src/cli.js @@ -5,7 +5,8 @@ import program from 'commander'; program .command('json2pot ') .option('-o, --output ', 'The output pathname of `.pot` file to be translated') - .option('-k, --message-key [key]', 'Translation message key (default key is `defaultMessage`)') + .option('-k, --message-key [key]', 'Translation message ID key (default key is `defaultMessage`)') + .option('-v, --message-value [key]', 'Translation message value key (default is to omit this)') .action(require('./extractAndWritePOTFromMessagesSync')); program @@ -15,7 +16,7 @@ program 'The pattern of *json* files extracted from *babel-plugin-react-intl*', ) .option('-o, --output ', 'The output pathname of a file / directory') - .option('-k, --message-key [key]', 'Translation message key (default key is `defaultMessage`)') + .option('-k, --message-key [key]', 'Translation message ID key (default key is `defaultMessage`)') .action(require('./filterPOAndWriteTranslateSync')); program.parse(process.argv); diff --git a/src/extractAndWritePOTFromMessagesSync.js b/src/extractAndWritePOTFromMessagesSync.js index 02ff370..29c6ac3 100644 --- a/src/extractAndWritePOTFromMessagesSync.js +++ b/src/extractAndWritePOTFromMessagesSync.js @@ -12,11 +12,11 @@ const customKeyMapper = (message, messageKey, filename) => ({ const customKeyMapperFactory = (messageKey = 'defaultMessage') => (message, filename) => customKeyMapper(message, messageKey, filename); -function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, output }) { +function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, messageValue, output }) { const mapper = messageKey ? customKeyMapperFactory(messageKey) : undefined; const result = flowRight( - potFormater, // 2. return formated string + potFormater(messageValue), // 2. return formated string readAllMessageAsObjectSync, // 1. return messages object )(srcPatterns, mapper); diff --git a/src/index.js b/src/index.js index 39b554a..0470311 100755 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,8 @@ import readAllPOAsObjectSync from './readAllPOAsObjectSync'; export default { extractAndWritePOTFromMessagesSync, filterPOAndWriteTranslateSync, - potFormater, + potFormater: potFormater(null), + potFormaterFactory: potFormater, readAllMessageAsObjectSync, readAllPOAsObjectSync, }; diff --git a/src/potFormater.js b/src/potFormater.js index f5abcd7..e5c3461 100644 --- a/src/potFormater.js +++ b/src/potFormater.js @@ -22,10 +22,10 @@ const potCommentsFormater = (messageList) => * @author Michael Hsu */ -const potFormater = (messageObject) => +const potFormater = (messageValue) => (messageObject) => Object.keys(messageObject) // return array of id .sort() - .map(id => `${potCommentsFormater(messageObject[id])}msgid "${id}"\nmsgstr ""\n`) + .map(id => `${potCommentsFormater(messageObject[id])}msgid "${id}"\nmsgstr "${messageValue ? messageObject[id][0][messageValue] : ''}"\n`) .join('\n'); export default potFormater; diff --git a/test/potFormater.test.js b/test/potFormater.test.js index 4a8d7c7..3e807f0 100644 --- a/test/potFormater.test.js +++ b/test/potFormater.test.js @@ -2,12 +2,12 @@ import test from 'ava'; import potFormater from '../src/potFormater'; test('should return a function', t => { - t.is(typeof potFormater, 'function'); + t.is(typeof potFormater(null), 'function'); }); test('should return pot formatted string', t => { t.is( - potFormater({ + potFormater(null)({ 'Go to MCS website': [ { id: 'App.errorButton',