Socket
Socket
Sign inDemoInstall

gettext-parser

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gettext-parser - npm Package Compare versions

Comparing version 0.1.10 to 0.2.0

CHANGELOG.md

1

index.js

@@ -0,1 +1,2 @@

"use strict";

@@ -2,0 +3,0 @@ module.exports = {

32

lib/mocompiler.js

@@ -0,1 +1,3 @@

"use strict";
var encoding = require("encoding"),

@@ -17,3 +19,3 @@ sharedFuncs = require("./shared");

/**
* Creates a MO compiler object.
* Creates a MO compiler object.
*

@@ -27,3 +29,3 @@ * @constructor

this._table.translations = this._table.translations || {};
this._translations = [];

@@ -57,3 +59,3 @@

if(!charset){
charset = sharedFuncs.formatCharset(value.trim() || "utf-8");
charset = sharedFuncs.formatCharset(value.trim() || "utf-8");
}

@@ -78,6 +80,6 @@ return "charset=" + charset;

/**
* Generates an array of translation strings
* Generates an array of translation strings
* in the form of [{msgid:... , msgstr:...}]
*
* @return {Array} Translation strings array
* @return {Array} Translation strings array
*/

@@ -119,3 +121,3 @@ Compiler.prototype._generateList = function(){

list.push({
msgid: encoding.convert(key, this._charset),
msgid: encoding.convert(key, this._charset),
msgstr: encoding.convert(value, this._charset)

@@ -173,24 +175,24 @@ });

i, len;
// magic
returnBuffer[this._writeFunc](this.MAGIC, 0);
// revision
returnBuffer[this._writeFunc](0, 4);
// string count
returnBuffer[this._writeFunc](list.length, 8);
// original string table offset
returnBuffer[this._writeFunc](28, 12);
// translation string table offset
returnBuffer[this._writeFunc](28 + (4+4) * list.length, 16);
// hash table size
returnBuffer[this._writeFunc](0, 20);
// hash table offset
returnBuffer[this._writeFunc](28 + (4+4) * list.length, 24);
// build originals table

@@ -205,3 +207,3 @@ curPosition = 28 + 2 * (4 + 4) * list.length;

}
// build translations table

@@ -208,0 +210,0 @@ for(i=0, len = list.length; i<len; i++){

@@ -0,1 +1,3 @@

"use strict";
var encoding = require("encoding"),

@@ -24,3 +26,3 @@ sharedFuncs = require("./shared");

function Parser(fileContents, defaultCharset){
this._fileContents = fileContents;

@@ -32,3 +34,3 @@

this._writeFunc = "writeUInt32LE";
/**

@@ -38,5 +40,5 @@ * Method name for reading int32 values, default littleendian

this._readFunc = "readUInt32LE";
this._charset = defaultCharset || "iso-8859-1";
this._charset = defaultCharset || "iso-8859-1";
this._table = {

@@ -46,3 +48,3 @@ charset: this._charset,

translations: {}
};
};
}

@@ -57,3 +59,3 @@

* Checks if number values in the input file are in big- or littleendian format.
*
*
* @return {Boolean} Return true if magic was detected

@@ -84,3 +86,3 @@ */

msgid, msgstr;
for(var i = 0; i < this._total; i++){

@@ -93,3 +95,3 @@ // msgid string

msgid = this._fileContents.slice(position, position + length);
// matching msgstr

@@ -101,7 +103,7 @@ length = this._fileContents[this._readFunc](offsetTranslations);

msgstr = this._fileContents.slice(position, position + length);
if(!i && !msgid.toString()){
this._handleCharset(msgstr);
}
msgid = encoding.convert(msgid, "utf-8", this._charset).toString("utf-8");

@@ -112,3 +114,3 @@ msgstr = encoding.convert(msgstr, "utf-8", this._charset).toString("utf-8");

}
// dump the file contents object

@@ -120,3 +122,3 @@ this._fileContents = null;

* Detects charset for MO strings from the header
*
*
* @param {Buffer} headers Header value

@@ -128,7 +130,7 @@ */

match;
if((match = headersStr.match(/[; ]charset\s*=\s*([\w\-]+)/i))){
this._charset = this._table.charset = sharedFuncs.formatCharset(match[1], this._charset);
}
headers = encoding.convert(headers, "utf-8", this._charset).toString("utf-8");

@@ -168,7 +170,7 @@

translation.msgstr = [].concat(msgstr || []);
if(!this._table.translations[msgctxt]){
this._table.translations[msgctxt] = {};
}
this._table.translations[msgctxt][msgid] = translation;

@@ -191,8 +193,8 @@ };

this._revision = this._fileContents[this._readFunc](4);
/**
* Total count of translated strings
*/
this._total = this._fileContents[this._readFunc](8);
this._total = this._fileContents[this._readFunc](8);
/**

@@ -202,3 +204,3 @@ * Offset position for original strings table

this._offsetOriginals = this._fileContents[this._readFunc](12);
/**

@@ -205,0 +207,0 @@ * Offset position for translation strings table

@@ -0,1 +1,3 @@

"use strict";
var encoding = require("encoding"),

@@ -26,3 +28,3 @@ sharedFuncs = require("./shared");

this._table.translations = this._table.translations || {};
this._translations = [];

@@ -130,7 +132,8 @@

Compiler.prototype._addPOString = function(key, value){
var line;
key = (key || "").toString();
// escape newlines and quotes
value = (value || "").toString().
value = (value || "").toString().
replace(/\\/g, "\\\\").

@@ -158,3 +161,3 @@ replace(/\"/g, "\\\"").

}
return key + " " + line;

@@ -179,3 +182,3 @@ };

if(!charset){
charset = sharedFuncs.formatCharset(value.trim() || "utf-8");
charset = sharedFuncs.formatCharset(value.trim() || "utf-8");
}

@@ -205,6 +208,6 @@ return "charset=" + charset;

Compiler.prototype.compile = function(){
var response = [],
headerBlock = this._table.translations[""] && this._table.translations[""][""] || {};
response.push(this._drawBlock(headerBlock, {msgstr: sharedFuncs.generateHeader(this._table.headers)}));

@@ -233,2 +236,2 @@

}
};
};

@@ -0,1 +1,3 @@

"use strict";
var encoding = require("encoding"),

@@ -17,3 +19,3 @@ sharedFuncs = require("./shared");

/**
* Creates a PO parser object. If PO object is a string,
* Creates a PO parser object. If PO object is a string,
* UTF-8 will be used as the charset

@@ -26,3 +28,3 @@ *

function Parser(fileContents, defaultCharset){
this._charset = defaultCharset || "iso-8859-1";

@@ -41,3 +43,3 @@

* Detects charset for PO strings from the header
*
*
* @param {Buffer} headers Header value

@@ -48,3 +50,3 @@ */

pos, headers = "", match;
if((pos = str.search(/^\s*msgid/im))>=0){

@@ -55,3 +57,3 @@ if((pos = pos + str.substr(pos + 5).search(/^\s*(msgid|msgctxt)/im))){

}
if((match = headers.match(/[; ]charset\s*=\s*([\w\-]+)(?:[\s;]|\\n)*"\s*$/mi))){

@@ -108,3 +110,3 @@ this._charset = sharedFuncs.formatCharset(match[1], this._charset);

state = this.states.none;
for(var i=0, len = this._fileContents.length; i<len; i++){

@@ -177,3 +179,3 @@ chr = this._fileContents.charAt(i);

}
return lex;

@@ -217,18 +219,18 @@ };

comment = {translator: [], extracted: [], reference: [], flag: [], previous: []};
lines = (node.value || "").split(/\n/);
lines = (node.value || "").split(/\n/);
lines.forEach(function(line){
switch(line.charAt(0) || ""){
case ":":
case ":":
comment.reference.push(line.substr(1).trim());
break;
case ".":
case ".":
comment.extracted.push(line.substr(1).replace(/^\s+/, ""));
break;
case ",":
case ",":
comment.flag.push(line.substr(1).replace(/^\s+/, ""));
break;
case "|":
case "|":
comment.previous.push(line.substr(1).replace(/^\s+/, ""));
break;
default:
default:
comment.translator.push(line.replace(/^\s+/, ""));

@@ -293,15 +295,15 @@ }

};
if(curContext){
lastNode.msgctxt = curContext;
}
if(curComments){
lastNode.comments = curComments;
}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;

@@ -314,7 +316,7 @@ curComments = false;

}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;

@@ -326,7 +328,7 @@ curComments = false;

}
if(lex[i].comments && !lastNode.comments){
lastNode.comments = lex[i].comments;
}
curContext = false;

@@ -350,3 +352,3 @@ curComments = false;

charset: this._charset,
headers: undefined,
headers: undefined,
translations: {}

@@ -379,3 +381,3 @@ };

var lex = this._lexer();
lex = this._joinStringValues(lex);

@@ -382,0 +384,0 @@ this._parseComments(lex);

@@ -0,1 +1,2 @@

"use strict";

@@ -33,3 +34,3 @@ // Expose to the world

* Convert first letters after - to uppercase, other lowercase
*
*
* @param {String} str String to be updated

@@ -74,3 +75,3 @@ * @return {String} A string with uppercase words

replace(/^(us[\-_]?)?ascii$/, "ascii").
replace(/^charset$/, defaultCharset || "iso-8859-1").
replace(/^charset$/, defaultCharset || "iso-8859-1").
trim();

@@ -121,2 +122,2 @@ }

return lines;
}
}
{
"name": "gettext-parser",
"description": "Parse and compile gettext po and mo files to/from json, nothing more, nothing less",
"version": "0.1.10",
"version": "0.2.0",
"author" : "Andris Reinman",

@@ -19,5 +19,2 @@ "homepage": "http://github.com/andris9/gettext-parser",

},
"optionalDependencies":{
"iconv": "*"
},
"devDependencies": {

@@ -24,0 +21,0 @@ "nodeunit": "*"

@@ -9,6 +9,8 @@ gettext-parser

This module is slightly based on my other gettext related module [node-gettext](https://github.com/andris9/node-gettext). The plan is to move all parsing and compiling logic from node-gettext to here and leave only translation related functions (domains, plural handling, lookups etc.).
This module is slightly based on my other gettext related module [node-gettext](https://github.com/andris9/node-gettext). The plan is to move all parsing and compiling logic from node-gettext to here and leave only translation related functions (domains, plural handling, lookups etc.).
If you get a bunchload of warnings or (non fatal) errors when installing, it is ok. These are most probably generated by the optional iconv dependency.
## ICONV NOTICE
By default *gettext-parser* uses pure JS [iconv-lite](https://github.com/ashtuchkin/iconv-lite) for encoding and decoding non UTF-8 charsets. If you need to support more complex encodings like EUC or Shift_JIS, you need to add [iconv](https://github.com/bnoordhuis/node-iconv) as a dependency for your project.
## Usage

@@ -23,3 +25,3 @@

* `gettextParser.po.parse(buf[, defaultCharset])` where `buf` is a *po* file as a Buffer or an unicode string. `defaultCharset` is the charset to use if charset is not defined or is the default `"CHARSET"`. Returns gettext-parser specific translation object (see below)
* `gettextParser.po.compile(obj)` where `obj` is a translation object, returns a *po* file as a Buffer
* `gettextParser.po.compile(obj)` where `obj` is a translation object, returns a *po* file as a Buffer
* `gettextParser.mo.parse(buf[, defaultCharset])` where `buf` is a *mo* file as a Buffer (*mo* is binary format, so do not use strings). `defaultCharset` is the charset to use if charset is not defined or is the default `"CHARSET"`. Returns translation object

@@ -57,3 +59,3 @@ * `gettextParser.mo.compile(obj)` where `obj` is a translation object, returns a *mo* file as a Buffer

* **msgstr** an array of translations
* **comments** an object with the following properties: `translator`, `reference`, `extracted`, `flag`, `previous`.
* **comments** an object with the following properties: `translator`, `reference`, `extracted`, `flag`, `previous`.

@@ -83,3 +85,3 @@ Example

"msgid": "%s example",
"msgid_plural": "%s examples",
"msgid_plural": "%s examples",
"msgstr": ["% näide", "%s näidet"],

@@ -98,4 +100,6 @@ "comments": {

If you need to convert *gettext-parser* formatted translation object to something else, eg. for *jed*, check out [po2json](https://github.com/mikeedwards/po2json).
## License
**MIT**

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc