forked from jruchaud/babel-gettext-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (81 loc) · 3.63 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict";
var gettextParser = require("gettext-parser");
var fs = require("fs");
var DEFAULT_FUNCTION_NAMES = {
gettext: ["msgid"],
dgettext: ["domain", "msgid"],
ngettext: ["msgid", "msgid_plural", "count"],
dngettext: ["domain", "msgid", "msgid_plural", "count"],
pgettext: ["msgctxt", "msgid"],
dpgettext: ["domain", "msgctxt", "msgid"],
npgettext: ["msgctxt", "msgid", "msgid_plural", "count"],
dnpgettext: ["domain", "msgctxt", "msgid", "msgid_plural", "count"]
};
var DEFAULT_FILE_NAME = "gettext.po";
var DEFAULT_HEADERS = {
"content-type": "text/plain; charset=UTF-8",
"plural-forms": "nplurals = 2; plural = (n !== 1);"
};
exports.__esModule = true;
exports.default = function() {
var currentFileName;
var data;
return {visitor: {
CallExpression(nodePath, plugin) {
var functionNames = plugin.opts && plugin.opts.functionNames || DEFAULT_FUNCTION_NAMES;
var fileName = plugin.opts && plugin.opts.fileName || DEFAULT_FILE_NAME;
var headers = plugin.opts && plugin.opts.headers || DEFAULT_HEADERS;
var defaultTranslate = plugin.opts && plugin.opts.defaultTranslate;
if (fileName !== currentFileName) {
currentFileName = fileName;
data = {
charset: "UTF-8",
headers: headers,
translations: {
context: {
}
}
};
headers["plural-forms"] = headers["plural-forms"] || DEFAULT_HEADERS["plural-forms"];
headers["content-type"] = headers["content-type"] || DEFAULT_HEADERS["content-type"];
}
var defaultContext = data.translations.context;
var nplurals = /nplurals ?= ?(\d)/.exec(headers["plural-forms"])[1];
let callee = nodePath.node.callee;
if (functionNames.hasOwnProperty(callee.name)
|| callee.property && functionNames.hasOwnProperty(callee.property.name)) {
var functionName = functionNames[callee.name] || functionNames[callee.property.name];
var translate = {};
var args = nodePath.get('arguments');
for (var i = 0, l = args.length; i < l; i++) {
var name = functionName[i];
if (name && name !== "count" && name !== "domain") {
var arg = args[i].evaluate();
var value = arg.value;
if (arg.confident && value) {
translate[name] = value;
}
if (name === "msgid_plural") {
translate.msgstr = [];
for (var p = 0; p < nplurals; p++) {
translate.msgstr[p] = defaultTranslate ? value : "";
}
} else if (name === "msgid") {
translate.msgstr = defaultTranslate ? value : "";
}
}
}
var context = defaultContext;
var msgctxt = translate.msgctxt;
if (msgctxt) {
data.translations[msgctxt] = data.translations[msgctxt] || {};
context = data.translations[msgctxt];
}
context[translate.msgid] = translate;
var output = gettextParser.po.compile(data);
fs.writeFileSync(fileName, output);
}
}
}};
};
module.exports = exports.default;