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

Add additional argument for generating msgstr in order to create *.po files #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

```
Expand Down
5 changes: 3 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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`)')
.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
Expand All @@ -15,7 +16,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`)')
.option('-k, --message-key [key]', 'Translation message ID key (default key is `defaultMessage`)')
.action(require('./filterPOAndWriteTranslateSync'));

program.parse(process.argv);
4 changes: 2 additions & 2 deletions src/extractAndWritePOTFromMessagesSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import readAllPOAsObjectSync from './readAllPOAsObjectSync';
export default {
extractAndWritePOTFromMessagesSync,
filterPOAndWriteTranslateSync,
potFormater,
potFormater: potFormater(null),
potFormaterFactory: potFormater,
readAllMessageAsObjectSync,
readAllPOAsObjectSync,
};
4 changes: 2 additions & 2 deletions src/potFormater.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Copy link
Owner

@evenchange4 evenchange4 Oct 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an eslint error here, please fix it or alternatively just adding one line eslint-disable for pretty display. :)

ps: Rebase onto master branch which now using node v6 and npm v3 and just run npm run lint locally.

.join('\n');

export default potFormater;
4 changes: 2 additions & 2 deletions test/potFormater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add another test case for custom messageValue will be great. :)

'Go to MCS website': [
{
id: 'App.errorButton',
Expand Down