Skip to content

Commit

Permalink
Merge pull request #2 from 2gis/allow-x-header
Browse files Browse the repository at this point in the history
Allow x header
  • Loading branch information
lastw authored Sep 25, 2018
2 parents 9406625 + fbbf6c4 commit b3e77a3
Show file tree
Hide file tree
Showing 8 changed files with 1,886 additions and 1,692 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
node_modules/
yarn-error.log
npm-debug.log

36 changes: 23 additions & 13 deletions dist/src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var occurenceRegex = /^\s*#\s?:\s?(.*)$/i;
function splitInTwo(src, separator) {
if (separator === void 0) { separator = ' '; }
var i = src.indexOf(separator);
if (i === -1) {
if (i === -1) { // no separator
return [src, ''];
}
return [src.slice(0, i), src.slice(i + 1)];
Expand All @@ -17,9 +17,13 @@ function convert(data, opts) {
var entries = data.split("\n\n").filter(function (e) { return !!e; });
// first entry should be header
var header = entries.shift();
var items = entries.reduce(function (acc, entry) {
var e = parseEntry(entry, opts.withComments, opts.withOccurences);
return e ? acc.concat(e) : acc;
}, []);
return {
meta: parseHeader(header, opts),
items: entries.map(function (entry) { return parseEntry(entry, opts.withComments, opts.withOccurences); })
meta: header ? parseHeader(header, opts) : undefined,
items: items
};
}
exports.convert = convert;
Expand All @@ -36,7 +40,7 @@ function parseHeader(header, opts) {
panic_1.panic("Malformed string: can't parse: ", [e.message]);
return;
}
var headers = result.msgStr.split("\n");
var headers = result.msgStr ? result.msgStr.split("\n") : [];
if (opts.withMeta === 'plural') {
var pluralHeader = headers.filter(function (headerItem) {
return headerItem.indexOf("Plural-Forms") === 0;
Expand All @@ -50,6 +54,9 @@ function parseHeader(header, opts) {
};
}
return headers.reduce(function (acc, header) {
if (header === '') {
return acc;
}
var _a = splitInTwo(header, ':').map(function (v) { return v.trim(); }), name = _a[0], value = _a[1];
switch (name) {
case "Project-Id-Version":
Expand Down Expand Up @@ -98,7 +105,8 @@ function parseHeader(header, opts) {
acc.generatedBy = value;
break;
default:
if (name) {
// allow X- header
if (!/^X-/.test(name)) {
panic_1.warning('PO header: unknown clause', [name, value]);
}
}
Expand All @@ -117,13 +125,17 @@ function parseEntry(entry, withComments, withOccurences) {
return;
}
var comments = result.comments, occurences = result.occurences, context = result.context, msgid = result.msgid, msgidPlural = result.msgidPlural, msgStr = result.msgStr, msgStrPlural = result.msgStrPlural;
if (!msgid) {
panic_1.panic('Invalid single entry: empty msgid string', entries);
return;
}
if (msgidPlural || msgStrPlural.length > 0) {
if (!msgidPlural || msgStrPlural.length == 0) {
panic_1.panic('Invalid plural entry: absent msgid_plural or msgstr[N] strings', [msgid, msgidPlural]);
panic_1.panic('Invalid plural entry: absent msgid_plural or msgstr[N] strings', [msgid].concat(entries));
return;
}
if (msgStrPlural.length !== msgStrPlural.filter(function (v) { return !!v; }).length) {
panic_1.warning('Some of plural strings are untranslated', msgStrPlural);
panic_1.warning('Some of plural strings are untranslated', [msgid].concat(msgStrPlural));
}
// valid plural form
return {
Expand All @@ -135,10 +147,6 @@ function parseEntry(entry, withComments, withOccurences) {
comments: comments.length > 0 ? comments : undefined
};
}
if (!msgid) {
panic_1.panic('Invalid single entry: empty msgid string', [msgid]);
return;
}
if (!msgStr) {
panic_1.warning('String is untranslated', [msgid]);
}
Expand Down Expand Up @@ -185,7 +193,8 @@ function _parse(entries, withComments, withOccurences) {
// msgstr[N] instruction explicit handler
var pluralMatch = lastMode.match(/msgstr\[(\d+)\]/i);
if (pluralMatch) {
msgStrPlural[pluralMatch[1]] += JSON.parse(entry);
var idx = parseInt(pluralMatch[1], 10);
msgStrPlural[idx] += JSON.parse(entry);
}
break;
}
Expand Down Expand Up @@ -229,7 +238,8 @@ function _parse(entries, withComments, withOccurences) {
// msgstr[N] instruction explicit handler
var pluralMatch = instruction.match(/msgstr\[(\d+)\]/i);
if (pluralMatch) {
msgStrPlural[pluralMatch[1]] = JSON.parse(body);
var idx = parseInt(pluralMatch[1], 10);
msgStrPlural[idx] = JSON.parse(body);
}
break;
}
Expand Down
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18n-po-json",
"version": "1.0.6",
"version": "1.0.7",
"description": "i18n .po to .json file converter",
"main": "dist/index.js",
"scripts": {
Expand All @@ -22,27 +22,29 @@
"license": "MIT",
"devDependencies": {
"@types/get-stdin": "^5.0.1",
"@types/mocha": "^5.2.5",
"@types/node": "8.9.4",
"@types/yargs": "^8.0.2",
"array-xor": "^0.1.1",
"browserify": "^14.4.0",
"browserify": "^16.2.2",
"eslint": "3.13.1",
"i18n-proto": "1.0.5",
"karma": "1.7.0",
"karma": "^3.0.0",
"karma-browserify": "^5.1.1",
"karma-firefox-launcher": "^1.0.1",
"karma-mocha": "^1.3.0",
"karma-typescript": "^3.0.4",
"mocha": "^3.4.2",
"ts-node": "^3.3.0",
"tslib": "1.5.0",
"karma-typescript": "^3.0.13",
"mocha": "^5.2.0",
"ts-node": "^7.0.1",
"tslib": "^1.9.3",
"tslint": "4.3.1",
"tslint-eslint-rules": "3.2.3",
"typescript": "^2.4.2",
"uglify-js": "3.0.18",
"typescript": "^2.9.2",
"uglify-js": "^3.4.9",
"watchify": "^3.9.0"
},
"dependencies": {
"get-stdin": "5.0",
"yargs": "8.0"
"get-stdin": "6.0.0",
"yargs": "8.0.0"
}
}
}
157 changes: 0 additions & 157 deletions project.d.ts

This file was deleted.

Loading

0 comments on commit b3e77a3

Please sign in to comment.