Skip to content

Commit

Permalink
Merge pull request #16 from lucbelliveau/master
Browse files Browse the repository at this point in the history
Duplicate fix, plural fix, multiple output files
  • Loading branch information
mrblueblue committed Oct 24, 2017
2 parents f169497 + 9b96f0c commit c981da3
Show file tree
Hide file tree
Showing 7 changed files with 3,236 additions and 6 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ module.exports = {

```

You can also specify a literal header prefix to include in the file, for comments or other things.

```
module.exports = {
header_prefix: '# this is my po file\nmsgid ""\nmsgstr ""'
```

Since 'gettext-loader' only parses Javascript (including ES6 and JSX), place it after loaders that transform some source to JS code.

```javascript
Expand Down Expand Up @@ -116,3 +123,17 @@ msgid "moring star"
msgtr ""

```
## Multiple PO File Output
If you want to use code splitting with your po files, you can configure `gettext-loader` to use the source file's name as part of its output file.
```javascript

module.exports = {
output: 'i18n/[filename]_en.po'
}

```
`[filename]` will be replaced with the filename where translations are found.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"babel-preset-stage-0": "^6.1.18",
"babel-cli": "^6.2.1",
"chai": "^3.4.1",
"coffee-loader": "^0.7.2",
"dot-loader": "^0.1.1",
Expand Down
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ module.exports = function(source) {
if (this.cacheable){
this.cacheable();
}
const relativeFilename = path.relative(root, this.resourcePath);

if (relativeFilename.indexOf('..') >= 0) {
// don't consider files outside of root
return source;
}

const output = {
path: `${root}/${config.output || 'en.po'}`
path: `${root}/${config.output.replace('[filename]', relativeFilename) || 'en.po'}`
}

const methodNames = config.methods || [DEFAULT_GETTEXT];
Expand Down Expand Up @@ -58,9 +64,10 @@ module.exports = function(source) {
}

} catch (error) {
const header_prefix = config.header_prefix || '';
const header = formatHeader(config.header);
const body = formatTranslations(translations);
output.source = `${header}\n${body}`
output.source = `${header_prefix}\n${header}\n${body}`

mkdirp.sync(getFolderPath(output.path));
fs.writeFileSync(output.path, output.source);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/buildMessageBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ const config = require(path.join(root, 'gettext.config.js'));
export const buildMsgstr = map((num) => `msgstr[${num}] ""\n`)
export const buildMsgstrs = (num) => cx(join(''), buildMsgstr)(range(0, num))
export const getNumPlurals = cx(parseInt, last, head, split(';'))

export const formatMessageBlock = (accum, translation) => {
const path = makeRelativePath(translation.path);

const translationBlock = cat(
let translationBlock = cat(
`#: ${path} ${translation.loc.line}:${translation.loc.column}\n`,
`msgid "${translation.text}"`
)

if (isPluralForm(translation.text)){
translationBlock += `\nmsgid_plural ""`
const msgstrs = cx(
buildMsgstrs,
getNumPlurals
Expand Down
5 changes: 3 additions & 2 deletions src/utils/extractTranslations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {filter, map, curry, compose, prop, head} from 'ramda';
import {uniqBy, map, curry, compose, prop, head} from 'ramda';
import {filterTreeForMethodsAndFunctionsNamed} from 'estree-utils';

const extractTranslations = (...args) => (ast) => {
Expand All @@ -24,7 +24,8 @@ const extractTranslations = (...args) => (ast) => {
}
}

return map(addLocation)(translationStrings);
const unique = s => gettextLocations[translationStrings.indexOf(s)];
return map(addLocation)(uniqBy(unique)(translationStrings));
}

export default extractTranslations;
2 changes: 2 additions & 0 deletions test/buildMessageBlocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('buildMessageBlocks', () => {
"\n",
'#: /here 1:5\n',
'msgid "%d views"\n',
'msgid_plural ""\n',
'msgstr[0] ""\n',
'msgstr[1] ""\n\n'
]
Expand Down Expand Up @@ -154,6 +155,7 @@ describe('buildMessageBlocks', () => {
const expected = [
'#: /here 1:5',
'msgid "%d views"',
'msgid_plural ""',
'msgstr[0] ""',
'msgstr[1] ""\n\n'
]
Expand Down
Loading

0 comments on commit c981da3

Please sign in to comment.