Skip to content

Commit

Permalink
Custom key mapper added (#41)
Browse files Browse the repository at this point in the history
feat(messageKey): Added feature where you can pass the message key and use something else than defaultMessage. [@janzal](https://github.com/janzal) in [#41]
  • Loading branch information
janzal authored and evenchange4 committed Oct 21, 2016
1 parent 7cc99d4 commit 0f2c9d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import program from 'commander';
program
.command('json2pot <srcPatterns>')
.option('-o, --output <path>', '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
Expand All @@ -14,6 +15,7 @@ program
'The pattern of *json* files extracted from *babel-plugin-react-intl*',
)
.option('-o, --output <path>', '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);
13 changes: 11 additions & 2 deletions src/extractAndWritePOTFromMessagesSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import flowRight from 'lodash/flowRight';
import readAllMessageAsObjectSync from './readAllMessageAsObjectSync';
import potFormater from './potFormater';

function extractAndWritePOTFromMessagesSync(srcPatterns, { output }) {
const customKeyMapper = (message, messageKey, filename) => ({
[message[messageKey]]: [{ ...message, filename }],
});

const customKeyMapperFactory = (messageKey = 'defaultMessage') =>
(message, filename) => customKeyMapper(message, messageKey, filename);

function extractAndWritePOTFromMessagesSync(srcPatterns, { messageKey, output }) {
const mapper = messageKey ? customKeyMapperFactory(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`));
Expand Down
7 changes: 4 additions & 3 deletions src/filterPOAndWriteTranslateSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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
Expand All @@ -22,8 +23,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]],
})),
}));

Expand Down
29 changes: 29 additions & 0 deletions test/extractAndWritePOTFromMessagesSync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

0 comments on commit 0f2c9d1

Please sign in to comment.