react-intl-po
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -6,2 +6,7 @@ # react-intl-po | ||
## [v1.0.10] | ||
> Oct 21, 2016 | ||
* feat(messageKey): Added feature where you can pass the message key and use something else than defaultMessage. ([@janzal](https://github.com/janzal) in [#41]) | ||
## [v1.0.9] | ||
@@ -8,0 +13,0 @@ > Sep 21, 2016 |
@@ -10,6 +10,6 @@ #!/usr/bin/env node | ||
_commander2.default.command('json2pot <srcPatterns>').option('-o, --output <path>', 'The output pathname of `.pot` file to be translated').action(require('./extractAndWritePOTFromMessagesSync')); | ||
_commander2.default.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')); | ||
_commander2.default.command('po2json <srcPatterns>').option('-m, --messages-pattern <path>', 'The pattern of *json* files extracted from *babel-plugin-react-intl*').option('-o, --output <path>', 'The output pathname of a file / directory').action(require('./filterPOAndWriteTranslateSync')); | ||
_commander2.default.command('po2json <srcPatterns>').option('-m, --messages-pattern <path>', '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')); | ||
_commander2.default.parse(process.argv); |
@@ -7,2 +7,5 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /* eslint-disable no-console */ | ||
var _fs = require('fs'); | ||
@@ -30,12 +33,29 @@ | ||
function extractAndWritePOTFromMessagesSync(srcPatterns, _ref) { | ||
var output = _ref.output; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var customKeyMapper = function customKeyMapper(message, messageKey, filename) { | ||
return _defineProperty({}, message[messageKey], [_extends({}, message, { filename: filename })]); | ||
}; | ||
var customKeyMapperFactory = function customKeyMapperFactory() { | ||
var messageKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'defaultMessage'; | ||
return function (message, filename) { | ||
return customKeyMapper(message, messageKey, filename); | ||
}; | ||
}; | ||
function extractAndWritePOTFromMessagesSync(srcPatterns, _ref2) { | ||
var messageKey = _ref2.messageKey; | ||
var output = _ref2.output; | ||
var mapper = messageKey ? customKeyMapperFactory(messageKey) : undefined; | ||
var result = (0, _flowRight2.default)(_potFormater2.default, // 2. return formated string | ||
_readAllMessageAsObjectSync2.default)(srcPatterns); | ||
_readAllMessageAsObjectSync2.default)(srcPatterns, mapper); | ||
_fs2.default.writeFileSync(output, result); | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + output + ' ✔️\n')); | ||
} /* eslint-disable no-console */ | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + output + ' \u2714\uFE0F\n')); | ||
} | ||
exports.default = extractAndWritePOTFromMessagesSync; | ||
module.exports = exports['default']; |
@@ -58,2 +58,4 @@ 'use strict'; | ||
function filterPOAndWriteTranslateSync(srcPatterns, _ref) { | ||
var _ref$messageKey = _ref.messageKey; | ||
var messageKey = _ref$messageKey === undefined ? 'defaultMessage' : _ref$messageKey; | ||
var messagesPattern = _ref.messagesPattern; | ||
@@ -69,6 +71,4 @@ var output = _ref.output; | ||
var result = (0, _toObjectBy2.default)(locales, function (locale) { | ||
return _defineProperty({}, locale, (0, _toObjectBy2.default)(messageList, function (_ref2) { | ||
var id = _ref2.id; | ||
var defaultMessage = _ref2.defaultMessage; | ||
return _defineProperty({}, id, translationTable[locale][defaultMessage]); | ||
return _defineProperty({}, locale, (0, _toObjectBy2.default)(messageList, function (message) { | ||
return _defineProperty({}, message.id, translationTable[locale][message[messageKey]]); | ||
})); | ||
@@ -80,3 +80,3 @@ }); | ||
_fs2.default.writeFileSync(output, JSON.stringify(result, null, 0)); | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + output + ' ✔️\n')); | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + output + ' \u2714\uFE0F\n')); | ||
} else { | ||
@@ -87,3 +87,3 @@ _mkdirp2.default.sync(output); // ensure the output folder exists | ||
_fs2.default.writeFileSync(_path2.default.join(output, lang + '.json'), JSON.stringify(result[lang], null, 0)); | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + _path2.default.join(output, lang + '.json') + ' ✔️')); | ||
console.log(_chalk2.default.green('> [react-intl-po] write file -> ' + _path2.default.join(output, lang + '.json') + ' \u2714\uFE0F')); | ||
return null; | ||
@@ -90,0 +90,0 @@ }); |
@@ -47,3 +47,3 @@ 'use strict'; | ||
function readAllMessageAsObjectSync(srcPatterns) { | ||
var messageMapper = arguments.length <= 1 || arguments[1] === undefined ? DEFAULT_MAPPER : arguments[1]; | ||
var messageMapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_MAPPER; | ||
@@ -50,0 +50,0 @@ return (0, _glob.sync)(srcPatterns) |
@@ -45,3 +45,3 @@ 'use strict'; | ||
function readAllPOAsObjectSync(srcPatterns) { | ||
var localeMapper = arguments.length <= 1 || arguments[1] === undefined ? DEFAULT_MAPPER : arguments[1]; | ||
var localeMapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_MAPPER; | ||
@@ -48,0 +48,0 @@ var filepaths = (0, _glob.sync)(srcPatterns); |
{ | ||
"name": "react-intl-po", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Extract POT from react-intl and convert back to json.", | ||
@@ -5,0 +5,0 @@ "author": "Michael Hsu", |
@@ -61,6 +61,7 @@ # react-intl-po | ||
| **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 | | ||
| **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`) | | ||
@@ -90,2 +91,3 @@ ### po2json | ||
| `output (-o)` | The output pathname of a file / directory | | ||
| `message-key (-k)` | [Optional] Translation message key (default key is `defaultMessage`) | | ||
@@ -100,3 +102,9 @@ | ||
## Q&A | ||
### How to translate the same message into two different meanings? | ||
- Set the `message-key (-k)` to `'key'` of message object from *babel-plugin-react-intl* (default key is `'defaultMessage'`). ([#41](https://github.com/evenchange4/react-intl-po/pull/41)) | ||
## Test | ||
@@ -103,0 +111,0 @@ |
@@ -8,2 +8,3 @@ #!/usr/bin/env node | ||
.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')); | ||
@@ -18,4 +19,5 @@ | ||
.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); |
@@ -8,7 +8,16 @@ /* eslint-disable no-console */ | ||
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); | ||
@@ -15,0 +24,0 @@ fs.writeFileSync(output, result); |
@@ -15,3 +15,4 @@ /* eslint-disable no-console */ | ||
function filterPOAndWriteTranslateSync(srcPatterns, { messagesPattern, output }) { | ||
function filterPOAndWriteTranslateSync(srcPatterns, | ||
{ messageKey = 'defaultMessage', messagesPattern, output }) { | ||
const translationTable = readAllPOAsObjectSync(srcPatterns); | ||
@@ -26,4 +27,4 @@ const messageList = flowRight( | ||
const result = toObjectBy(locales, locale => ({ | ||
[locale]: toObjectBy(messageList, ({ id, defaultMessage }) => ({ | ||
[id]: translationTable[locale][defaultMessage], | ||
[locale]: toObjectBy(messageList, (message) => ({ | ||
[message.id]: translationTable[locale][message[messageKey]], | ||
})), | ||
@@ -30,0 +31,0 @@ })); |
@@ -34,1 +34,30 @@ import fs from 'fs'; | ||
}); | ||
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' | ||
); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41475
720
126