/
buildMessageBlocks.js
55 lines (46 loc) · 1.18 KB
/
buildMessageBlocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import path from 'path';
import makeRelativePath from './makeRelativePath';
import isPluralForm from './isPluralForm';
import {
join,
concat as cat,
reduce,
last,
takeLast,
compose as cx,
map,
head,
split,
range
} from 'ramda';
const root = process.env.PWD;
const config = require(path.join(root, 'gettext.config.js'));
export const buildMsgtr = map((num) => `msgtr[${num}] ""\n`)
export const buildMsgtrs = (num) => cx(join(''), buildMsgtr)(range(0, num))
export const getNumPlurals = cx(parseInt, last, head, split(';'))
export const formatMessageBlock = (accum, translation) => {
const path = makeRelativePath(translation.path);
const translationBlock = cat(
`#: ${path} ${translation.loc.line}:${translation.loc.column}\n`,
`msgid "${translation.text}"`
)
if (isPluralForm(translation.text)){
const msgstrs = cx(
buildMsgtrs,
getNumPlurals
)(config.header['Plural-Forms'])
return cx(
cat(accum),
cat(translationBlock),
cat('\n'),
cat(msgstrs),
)('\n');
}
return cx(
cat(accum),
cat(translationBlock),
cat('\n'),
cat('msgtr ""\n'),
)('\n');
}
export default reduce(formatMessageBlock, '')