Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-gettext

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-gettext - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

181

lib/domain.js

@@ -64,26 +64,26 @@ var Iconv = require("iconv").Iconv;

// Check endianness of the file
this._checkMagick();
if(fileContents && fileContents.length){
// Check endianness of the file
this._checkMagick();
/**
* GetText revision nr, usually 0
*/
this.revision = this._fileContents[this._readFunc](4);
/**
* GetText revision nr, usually 0
*/
this.revision = this._fileContents[this._readFunc](4);
/**
* Total count of translated strings
*/
this.total = this._fileContents[this._readFunc](8);
/**
* Offset position for original strings table
*/
this._offsetOriginals = this._fileContents[this._readFunc](12);
/**
* Offset position for translation strings table
*/
this._offsetTranslations = this._fileContents[this._readFunc](16);
/**
* Total count of translated strings
*/
this.total = this._fileContents[this._readFunc](8);
/**
* Offset position for original strings table
*/
this._offsetOriginals = this._fileContents[this._readFunc](12);
/**
* Offset position for translation strings table
*/
this._offsetTranslations = this._fileContents[this._readFunc](16);
if(fileContents){
// Load translations into this._translationTable

@@ -282,2 +282,4 @@ this._loadTranslationTable();

replace(/([^&])&([^&])/g, "$1 $2"). // sanitize bitwise &
replace(/<</g, " "). // sanitize bitwise <<
replace(/>>/g, " "). // sanitize bitwise >>
trim();

@@ -296,2 +298,42 @@ if(str){

/**
* Generates header value for compiling
*
* @return {String} Header value
*/
GettextDomain.prototype._generateHeaders = function(){
return ["Project-Id-Version: node-gettext",
"MIME-Version: 1.0",
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: 8bit",
"Plural-Forms: " + this._pluralRules,
""].join("\n");
};
/**
* Creates a string value that can be included in PO file
*
* @param {String} key Key name
* @param {String} value Key value
* @return {String} Formatted string usable in PO files
*/
GettextDomain.prototype._addPOString = function(key, value){
var line = "";
key = (key || "").toString();
value = (value || "").toString().
replace(/\\/g, "\\\\").
replace(/\"/g, "\\\"").
replace(/\r/g, "\\r");
if(value.match(/\n/)){
value = value.replace(/\n/g, "\\n\n").replace(/\n$/, "");
line = ("\n"+value).split("\n").map(function(l){
return '"' + l + '"' + "\n";
}).join("");
}else{
line = '"' + value + '"' + "\n";
}
return key + " " + line;
};
/**
* Translates a string based of the context and number count.

@@ -333,10 +375,82 @@ *

this._translationTable[context][original] = translations;
}
};
/**
* Removes a translation from the translation table
*
* @param {String} context Translation context
* @param {String} original Original string
*/
GettextDomain.prototype.deleteTranslation = function(context, original){
original = (original || "").toString();
context = (context || "").toString().trim() || "";
if(this._translationTable[context] && this._translationTable[context][original]){
delete this._translationTable[context][original];
if(!Object.keys(this._translationTable[context]).length){
delete this._translationTable[context];
}
}
};
/**
* Compiles the current translation structure to a .PO file
*
* @return {Buffer} PO file contents, can be saved to disk etc.
*/
GettextDomain.prototype.compilePO = function(){
var translationTable = [],
returnStr = "";
// add header as the first field
translationTable.push({translation: [this._generateHeaders()]});
// gather all strings as key value pairs
Object.keys(this._translationTable).forEach((function(context){
Object.keys(this._translationTable[context]).forEach((function(original){
var originalArr = [];
for(var i=0, len = this._translationTable[context][original].length; i< len; i++){
originalArr[i] = original;
}
translationTable.push({
context: context,
original: originalArr,
translation: this._translationTable[context][original]
});
}).bind(this));
}).bind(this));
translationTable.forEach((function(line, i){
if(i){
returnStr += "\n";
}
if(line.context){
returnStr += this._addPOString("msgctxt", line.context);
}
returnStr += this._addPOString("msgid", line.original && line.original[0]);
if(line.original && line.original.length>1){
returnStr += this._addPOString("msgid_plural", line.original[1]);
}
if(line.translation.length<=1){
returnStr += this._addPOString("msgstr", line.translation[0]);
}else{
returnStr += line.translation.map((function(translation, i){
return this._addPOString("msgstr["+i+"]", translation);
}).bind(this)).join("");
}
}).bind(this));
return new Buffer(returnStr, "utf-8");
};
/**
* Compiles the current translation structure to a .MO file
*
* @return {Buffer} Binary MOD file contents, can be saved to disk etc.
* @return {Buffer} Binary MO file contents, can be saved to disk etc.
*/
GettextDomain.prototype.compile = function(){
GettextDomain.prototype.compileMO = function(){
var translationTable = [],

@@ -348,9 +462,6 @@ originalsLength = 0,

returnBuffer,
headers = ["Project-Id-Version: node-gettext",
"MIME-Version: 1.0",
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: 8bit",
"Plural-Forms: " + this._pluralRules,
""].join("\n");
translationTable.push(["", headers]);
i, len;
// add header as the first field
translationTable.push(["", this._generateHeaders()]);

@@ -365,3 +476,3 @@ // gather all strings as key value pairs

translationTable.push([
(context!=""?context+"\u0004":"")+originalArr.join("\u0000"),
(context ? context+"\u0004" : "")+originalArr.join("\u0000"),
this._translationTable[context][original].join("\u0000")

@@ -429,3 +540,3 @@ ]);

curPosition = 28 + 2 * (4+4) * translationTable.length;
for(var i=0, len = translationTable.length; i<len; i++){
for(i=0, len = translationTable.length; i<len; i++){
translationTable[i][0].copy(returnBuffer, curPosition);

@@ -439,3 +550,3 @@ returnBuffer[this._writeFunc](translationTable[i][0].length, 28 + i*8);

// build translations table
for(var i=0, len = translationTable.length; i<len; i++){
for(i=0, len = translationTable.length; i<len; i++){
translationTable[i][1].copy(returnBuffer, curPosition);

@@ -442,0 +553,0 @@ returnBuffer[this._writeFunc](translationTable[i][1].length, 28 + (4+4) * translationTable.length + i*8);

@@ -187,18 +187,46 @@

}
}
};
/**
* Removes a translation from the translation table
*
* @param {String} domain Original string
* @param {String} context Translation context
* @param {String} original Original string
*/
Gettext.prototype.deleteTranslation = function(domain, context, original){
domain = (domain || "").toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
textdomain.setTranslation(context, original);
}
};
/**
* Compiles a language table into a .MO file
*
* @return {Buffer} Binary MOD file contents, can be saved to disk etc.
* @return {Buffer} Binary MO file contents, can be saved to disk etc.
*/
Gettext.prototype.compile = function(domain){
Gettext.prototype.compileMO = function(domain){
domain = (domain || "").toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
return textdomain.compile();
return textdomain.compileMO();
}
}
};
/**
* Compiles a language table into a .PO file
*
* @return {Buffer} PO file contents, can be saved to disk etc.
*/
Gettext.prototype.compilePO = function(domain){
domain = (domain || "").toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
return textdomain.compilePO();
}
};
/**
* Registers gettext functions to String.prototype for easier (and global) access

@@ -298,2 +326,2 @@ */

}
}
};

2

package.json
{
"name": "node-gettext",
"description": "Gettext client for Node.js to use .mo files for I18N",
"version": "0.2.1",
"version": "0.2.2",
"author" : "Andris Reinman",

@@ -6,0 +6,0 @@ "maintainers":[

@@ -10,3 +10,3 @@ # node-gettext

* Add your own translations to the list
* Compile current translation table into a *MO* file!
* Compile current translation table into a *MO* or a *PO* file!

@@ -35,3 +35,3 @@ [![Build Status](https://secure.travis-ci.org/andris9/node-gettext.png)](http://travis-ci.org/andris9/node-gettext)

*addTextdomain(domain, file_contents)*
*addTextdomain(domain[, file_contents])*

@@ -123,2 +123,8 @@ var file_contents = fs.readFileSync("et.mo");

### Remove a translation
*deleteTranslation(domain, context, msgid, translation)*
gt.setTranslation("et", "", "Hello", "Tere");
### Compile

@@ -128,8 +134,14 @@

*compile([domain])*
*compileMO([domain])*
fs.writeFile("out.mo", gt.compile("et"));
fs.writeFile("out.mo", gt.compileMO("et"));
Compile current translation table to a PO file
*compilePO([domain])*
fs.writeFile("out.po", gt.compilePO("et"));
## License
MIT

@@ -283,5 +283,19 @@ var testCase = require('nodeunit').testCase,

deleteTranslation: function(test){
test.equal(this.g.gettext("o1"), "t1");
this.g.deleteTranslation("et", "", "o1");
test.equal(this.g.gettext("o1"), "o1");
test.done();
},
emptyFile: function(test){
this.g.addTextdomain("fi");
this.g.setTranslation("fi", "", "door", "ovi");
test.equal(this.g.dgettext("fi", "door"), "ovi");
test.done();
},
compile: function(test){
var g2 = new Gettext();
g2.addTextdomain("et", this.g.compile());
g2.addTextdomain("et", this.g.compileMO());
test.equal(this.g.gettext("o1"), g2.gettext("o1"));

@@ -288,0 +302,0 @@ test.done();

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