Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Where

* **data** is a translation object either got from parsing a PO/MO file or composed by other means
* **options** is a configuration object with possible values
* **foldLength** is the length at which to fold message strings into newlines (default: 76). Set to 0 or false to disable folding.
* **foldLength** is the page width at which to fold message strings into newlines (default: 79). Set to 0 or false to disable folding.
* **sort** (boolean|Function) - (default `false`) if `true`, entries will be sorted by msgid in the resulting .po(.pot) file.
If a comparator function is provided, that function will be used to sort entries in the output. The function is called with two arguments, each of which is a single message entry with the structure described below. The function should follow the standard rules for functions passed to `Array.sort()`: return `0` if the entries are interchangeable in sort order; return a number less than 0 if the first entry should come before the second one; and return a number greater than 0 if the second entry should come before the first one.
* **escapeCharacters** (boolean) - (default `true`) if `false`, will skip escape newlines and quotes characters functionality.
Expand Down
26 changes: 6 additions & 20 deletions lib/pocompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Compiler (table = {}, options = {}) {
this._table.headers = headers;

if (!('foldLength' in this._options)) {
this._options.foldLength = 76;
this._options.foldLength = 79;
}

if (!('escapeCharacters' in this._options)) {
Expand Down Expand Up @@ -154,7 +154,8 @@ Compiler.prototype._drawBlock = function (block, override = {}, obsolete = false
* @return {String} Joined and escaped key-value pair
*/
Compiler.prototype._addPOString = function (key = '', value = '', obsolete = false) {
key = key.toString();
const name = key.toString();
key = name;
if (obsolete) {
key = '#~ ' + key;
}
Expand All @@ -172,32 +173,17 @@ Compiler.prototype._addPOString = function (key = '', value = '', obsolete = fal

value = value.replace(/\n/g, '\\n'); // need to escape new line characters regardless

let lines = [value];
const lines = foldLength > 0 || escapeCharacters ? foldLine(value, foldLength, { name, obsolete }) : [value];

if (obsolete) {
eol = eol + '#~ ';
}

if (foldLength > 0) {
lines = foldLine(value, foldLength);
} else {
// split only on new lines
if (escapeCharacters) {
lines = value.split(/\\n/g);
for (let i = 0; i < lines.length - 1; i++) {
lines[i] = `${lines[i]}\\n`;
}
if (lines.length && lines[lines.length - 1] === '') {
lines.splice(-1, 1);
}
}
}

if (lines.length < 2) {
return `${key} "${lines.shift() || ''}"`;
return `${key} "${lines[0] || ''}"`;
}

return `${key} ""${eol}"${lines.join(`"${eol}"`)}"`;
return `${key} "${lines[0]}"${eol}"${lines.slice(1).join(`"${eol}"`)}"`;
};

/**
Expand Down
Loading
Loading