From be61d1ebcdd76763f3595441be78b7063e9da601 Mon Sep 17 00:00:00 2001 From: Jan Zaloudek Date: Wed, 19 Oct 2016 13:11:12 +0200 Subject: [PATCH 1/3] added support for custom message keys --- src/cli.js | 2 ++ src/extractAndWritePOTFromMessagesSync.js | 13 +++++++-- src/filterPOAndWriteTranslateSync.js | 6 ++-- ...extractAndWritePOTFromMessagesSync.test.js | 29 +++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/cli.js b/src/cli.js index 37ae2f7..1ecd467 100644 --- a/src/cli.js +++ b/src/cli.js @@ -5,6 +5,7 @@ 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`)') .action(require('./extractAndWritePOTFromMessagesSync')); program @@ -14,6 +15,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`)') .action(require('./filterPOAndWriteTranslateSync')); program.parse(process.argv); diff --git a/src/extractAndWritePOTFromMessagesSync.js b/src/extractAndWritePOTFromMessagesSync.js index 34cad54..7410244 100644 --- a/src/extractAndWritePOTFromMessagesSync.js +++ b/src/extractAndWritePOTFromMessagesSync.js @@ -5,11 +5,20 @@ import flowRight from 'lodash/flowRight'; import readAllMessageAsObjectSync from './readAllMessageAsObjectSync'; import potFormater from './potFormater'; -function extractAndWritePOTFromMessagesSync(srcPatterns, { output }) { +const CUSTOM_KEY_MAPPER = (message, messageKey, filename) => ({ + [message[messageKey]]: [{ ...message, filename }] +}); + +const defaultMapperFactory = (messageKey = 'defaultMessage') => + (message, filename) => CUSTOM_KEY_MAPPER(message, messageKey, filename); + +function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, output }) { + const mapper = messageKey ? defaultMapperFactory(messageKey) : undefined; + const result = flowRight( potFormater, // 2. return formated string readAllMessageAsObjectSync, // 1. return messages object - )(srcPatterns); + )(srcPatterns, mapper); fs.writeFileSync(output, result); console.log(chalk.green(`> [react-intl-po] write file -> ${output} ✔️\n`)); diff --git a/src/filterPOAndWriteTranslateSync.js b/src/filterPOAndWriteTranslateSync.js index 42b08db..7e230dc 100644 --- a/src/filterPOAndWriteTranslateSync.js +++ b/src/filterPOAndWriteTranslateSync.js @@ -12,7 +12,7 @@ import readAllPOAsObjectSync from './readAllPOAsObjectSync'; const isAJSONFile = string => /.json/.test(string); -function filterPOAndWriteTranslateSync(srcPatterns, { messagesPattern, output }) { +function filterPOAndWriteTranslateSync(srcPatterns, { messageKey = 'defaultMessage', messagesPattern, output }) { const translationTable = readAllPOAsObjectSync(srcPatterns); const messageList = flowRight( flatten, // 3. return flatten object values @@ -22,8 +22,8 @@ function filterPOAndWriteTranslateSync(srcPatterns, { messagesPattern, output }) const locales = Object.keys(translationTable); const result = toObjectBy(locales, locale => ({ - [locale]: toObjectBy(messageList, ({ id, defaultMessage }) => ({ - [id]: translationTable[locale][defaultMessage], + [locale]: toObjectBy(messageList, (message) => ({ + [message.id]: translationTable[locale][message[messageKey]], })), })); diff --git a/test/extractAndWritePOTFromMessagesSync.test.js b/test/extractAndWritePOTFromMessagesSync.test.js index b91f78b..70681d3 100644 --- a/test/extractAndWritePOTFromMessagesSync.test.js +++ b/test/extractAndWritePOTFromMessagesSync.test.js @@ -31,3 +31,32 @@ test('should return messages object with default mapper', t => { 'msgstr ""\n', ); }); + +test('should return messages object with custom message key mapper', t => { + const output = './temp/extract.pot'; + + extractAndWritePOTFromMessagesSync('./messages/**/*.json', { messageKey: 'id', output }); + t.is( + fs.readFileSync(output, 'utf8'), + '#: ./messages/src/containers/App/App.json\n' + + '#. [App.Creator] - Creator\n' + + 'msgid "App.Creator"\n' + + 'msgstr ""\n\n' + + '#: ./messages/src/containers/App/App.json\n' + + '#. [App.errorButton] - Click error Button\n' + + 'msgid "App.errorButton"\n' + + 'msgstr ""\n\n' + + '#: ./messages/src/containers/App/App.json\n' + + '#. [App.errorMessage] - The error message when api response as 404 not found\n' + + 'msgid "App.errorMessage"\n' + + 'msgstr ""\n\n' + + '#: ./messages/src/containers/NotFound/messages.json\n' + + '#. [NotFound.Creator] - Creator\n' + + 'msgid "NotFound.Creator"\n' + + 'msgstr ""\n\n' + + '#: ./messages/src/containers/NotFound/messages.json\n' + + '#. [NotFound.errorButton] - Click error Button\n' + + 'msgid "NotFound.errorButton"\n' + + 'msgstr ""\n' + ); +}); From bc853bc8c36f389ed0de5343978961054f9147d5 Mon Sep 17 00:00:00 2001 From: Jan Zaloudek Date: Thu, 20 Oct 2016 10:15:50 +0200 Subject: [PATCH 2/3] renamed mapper factory function --- src/extractAndWritePOTFromMessagesSync.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extractAndWritePOTFromMessagesSync.js b/src/extractAndWritePOTFromMessagesSync.js index 7410244..ae5b7b3 100644 --- a/src/extractAndWritePOTFromMessagesSync.js +++ b/src/extractAndWritePOTFromMessagesSync.js @@ -9,11 +9,11 @@ const CUSTOM_KEY_MAPPER = (message, messageKey, filename) => ({ [message[messageKey]]: [{ ...message, filename }] }); -const defaultMapperFactory = (messageKey = 'defaultMessage') => +const customKeyMapperFactory = (messageKey = 'defaultMessage') => (message, filename) => CUSTOM_KEY_MAPPER(message, messageKey, filename); function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, output }) { - const mapper = messageKey ? defaultMapperFactory(messageKey) : undefined; + const mapper = messageKey ? customKeyMapperFactory(messageKey) : undefined; const result = flowRight( potFormater, // 2. return formated string From eb8790fd0fbc8064c73f4758f58f124281b05f49 Mon Sep 17 00:00:00 2001 From: Jan Zaloudek Date: Thu, 20 Oct 2016 11:45:09 +0200 Subject: [PATCH 3/3] eslint issues fixed --- src/extractAndWritePOTFromMessagesSync.js | 6 +++--- src/filterPOAndWriteTranslateSync.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/extractAndWritePOTFromMessagesSync.js b/src/extractAndWritePOTFromMessagesSync.js index ae5b7b3..02ff370 100644 --- a/src/extractAndWritePOTFromMessagesSync.js +++ b/src/extractAndWritePOTFromMessagesSync.js @@ -5,12 +5,12 @@ import flowRight from 'lodash/flowRight'; import readAllMessageAsObjectSync from './readAllMessageAsObjectSync'; import potFormater from './potFormater'; -const CUSTOM_KEY_MAPPER = (message, messageKey, filename) => ({ - [message[messageKey]]: [{ ...message, filename }] +const customKeyMapper = (message, messageKey, filename) => ({ + [message[messageKey]]: [{ ...message, filename }], }); const customKeyMapperFactory = (messageKey = 'defaultMessage') => - (message, filename) => CUSTOM_KEY_MAPPER(message, messageKey, filename); + (message, filename) => customKeyMapper(message, messageKey, filename); function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, output }) { const mapper = messageKey ? customKeyMapperFactory(messageKey) : undefined; diff --git a/src/filterPOAndWriteTranslateSync.js b/src/filterPOAndWriteTranslateSync.js index 7e230dc..4298bc1 100644 --- a/src/filterPOAndWriteTranslateSync.js +++ b/src/filterPOAndWriteTranslateSync.js @@ -12,7 +12,8 @@ import readAllPOAsObjectSync from './readAllPOAsObjectSync'; const isAJSONFile = string => /.json/.test(string); -function filterPOAndWriteTranslateSync(srcPatterns, { messageKey = 'defaultMessage', messagesPattern, output }) { +function filterPOAndWriteTranslateSync(srcPatterns, + { messageKey = 'defaultMessage', messagesPattern, output }) { const translationTable = readAllPOAsObjectSync(srcPatterns); const messageList = flowRight( flatten, // 3. return flatten object values