Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom key mapper added #41

Merged
merged 3 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
});