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 overrideContext configuration #76

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Supports globs
* Supports flow type
* Supports string concatenation, e.g. `gettext('Foo ' + 'Bar')` (useful for wrapping into multiple lines)
* Supports dynamically overriding message contexts.

## Usage

Expand Down Expand Up @@ -301,6 +302,46 @@ Does not break long strings into several lines

Default: `false`

##### `overrideContext`

Overrides the context present in a component/function.

This allows for dynamic creation of messages for contexts.

Ex:

```
<GetText
message={'{{ name }} is very tall'}
context={gender}
/>
```

Configured with

```
const messages = extractMessages(code, {
overrideContext: ['male', 'female'],
})
```

Would result in

```
#: src/App.js:37
msgctxt "male"
msgid "{{ name }} is very tall"
msgstr ""

#: src/App.js:37
msgctxt "female"
msgid "{{ name }} is very tall"
msgstr ""
```

This allows your translation teams to create context specific translations, especially useful for languages that have variations based on gender.


### Configuration file

The `react-gettext-parser` CLI accepts a `--config <file path>` argument. This should point to a JavaScript or JSON file that exports an object with any or all of the available options as root properties. Here's an example:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"clean": "rimraf lib",
"dev": "babel -w --out-dir lib src",
"prebuild": "npm run clean",
"prepare": "npm run build",
"build": "babel --out-dir lib src"
},
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,18 @@ export const extractMessages = (code, opts = {}) => {
}))
}

if (Array.isArray(opts.overrideContext) && opts.overrideContext.length > 0) {
blocks = blocks.reduce((acc, block) => {
// If we havea msgctxt and it's no the default '' empty context (would be a literal or undefined if a variable)
if ("msgctxt" in block && block.msgctxt !== '') {
return [
...acc,
...opts.overrideContext.map((override) => ({...block, msgctxt: override}))
];
}
return [...acc, block];
}, []);
}
return blocks
}

Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/OverrideContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint react/prop-types: 0 */
import React from 'react';
import { GetText, npgettext } from 'gettext-lib';

export const Comp = ({ value }) => {
let context = 'foobar';
return (
<div>
<GetText
message={'Value is: {{ value }}'}
messagePlural={'Values are: {{ value }}'}
scope={{ value }}
comment={'Comment'}
context={context}
/>
<GetText
message={'No context here!'}
comment={'No context'}
/>
</div>
);
}
26 changes: 26 additions & 0 deletions tests/fixtures/OverrideContext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"msgctxt": "foo",
"msgid": "Translate me",
"comments": {
"reference": [],
"extracted": []
}
},
{
"msgctxt": "bar",
"msgid": "Translate me",
"comments": {
"reference": [],
"extracted": []
}
},
{
"msgctxt": "",
"msgid": "No context here!",
"comments": {
"reference": [],
"extracted": []
}
}
]
15 changes: 14 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('react-gettext-parser', () => {
it('should extract a message from jsx', () => {
const code = getSource('SingleString.jsx')
const messages = extractMessages(code)

const expected = getJson('SingleString.json')

expect(messages).to.have.length(1)
Expand Down Expand Up @@ -553,4 +552,18 @@ describe('react-gettext-parser', () => {
expect(messages[0].msgid).to.equal('Pipeline operator works')
})
})

describe('context override support', () => {
it('should override context', () => {
const code = getSource('OverrideContext.js')
const messages = extractMessages(code, {
overrideContext: ['foo', 'bar'],
})
const expected = getJson('OverrideContext.json')
expect(messages).to.have.length(3)
expect(messages[0].msgctxt).to.equal(expected[0].msgctxt)
expect(messages[1].msgctxt).to.equal(expected[1].msgctxt)
expect(messages[2].msgctxt).to.equal(expected[2].msgctxt)
})
})
})