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.14 to 1.0.0

.jshintrc

432

lib/gettext.js

@@ -0,5 +1,6 @@

'use strict';
var GettextDomain = require("./domain");
var plurals = require('./plurals');
var gettextParser = require('gettext-parser');
// expose to the world
module.exports = Gettext;

@@ -9,46 +10,45 @@

* Gettext function
*
*
* @constructor
*/
function Gettext(){
this._domains = {};
this._textdomain = false;
function Gettext() {
this.domains = {};
this._currentDomain = false;
}
////////////////// PRIVATE API //////////////////
/**
* Loads a translation from the TextDomain object
*
* @param {String} [domain] Case insensitive language identifier
* @param {String} msgid String to be translated
* @param {String} [context] Translation context
* @param {Number} [count] Number count for plural translations
* @return {String|Boolean} Returns the translation or false if not found
*/
Gettext.prototype._getTranslation = function(domain, msgid, context, count){
domain = (domain || this._textdomain || "").toLowerCase().trim();
if(this._domains[domain]){
return this._domains[domain].getTranslation(msgid, context, count);
}else{
return false;
}
};
////////////////// PUBLIC API //////////////////
/**
* Adds a gettext to the domains list. If default textdomain is not set, uses it
* as default
*
* @param {String} domain Case insensitive language identifier (domain)
*
* @param {String} domain Case insensitive language identifier (domain)
* @param {Buffer} fileContents Translations file (*.mo) contents as a Buffer object
*/
Gettext.prototype.addTextdomain = function(domain, fileContents){
domain = (domain || "").toString().toLowerCase().trim();
this._domains[domain] = new GettextDomain(domain, fileContents);
if(!this._textdomain){
this._textdomain = domain;
Gettext.prototype.addTextdomain = function(domain, file) {
domain = this._normalizeDomain(domain);
var translation;
if (file && typeof file !== 'string') {
translation = gettextParser.mo.parse(file, 'utf-8');
}
if (!translation) {
translation = gettextParser.po.parse(file || '', 'utf-8');
}
// We do not want to parse and compile stuff from unknown sources
// so we only use precompiled plural definitions
var pluralsInfo = plurals[this._normalizeDomain(domain, true)];
if (pluralsInfo) {
translation.headers['plural-forms'] = pluralsInfo.pluralsText;
translation.pluralsFunc = pluralsInfo.pluralsFunc;
} else {
// default plurals to EN rules
translation.pluralsFunc = plurals.en.pluralsFunc;
}
this.domains[domain] = translation;
if (!this._currentDomain) {
this._currentDomain = domain;
}
};

@@ -58,12 +58,18 @@

* Changes the current default textdomain
*
*
* @param {String} [domain] Case insensitive language identifier
* @return {String} cuurent textdomain
*/
Gettext.prototype.textdomain = function(domain){
domain = (domain || "").toString().toLowerCase().trim();
if(domain){
this._textdomain = domain;
Gettext.prototype.textdomain = function(updatedDomain) {
if (!arguments.length) {
return this._currentDomain;
}
return this._textdomain;
updatedDomain = this._normalizeDomain(updatedDomain);
if (this._currentDomain !== updatedDomain && this.domains.hasOwnProperty(updatedDomain)) {
this._currentDomain = updatedDomain;
return true;
} else {
return false;
}
};

@@ -73,8 +79,8 @@

* Translates a string using the default textdomain
*
*
* @param {String} msgid String to be translated
* @return {String} translation or the original string if no translation was found
*/
Gettext.prototype.gettext = function(msgid){
return this.dnpgettext(false, false, msgid);
Gettext.prototype.gettext = function(msgid) {
return this.dnpgettext(this._currentDomain, '', msgid);
};

@@ -84,14 +90,14 @@

* Translates a string using a specific domain
*
* @param {String} domain Case insensitive language identifier
*
* @param {String} domain Case insensitive language identifier
* @param {String} msgid String to be translated
* @return {String} translation or the original string if no translation was found
*/
Gettext.prototype.dgettext = function(domain, msgid){
return this.dnpgettext(domain, false, msgid);
Gettext.prototype.dgettext = function(domain, msgid) {
return this.dnpgettext(domain, '', msgid);
};
/**
* Translates a plural string using the default textdomain
*
* Translates a plural string using the default textdomain
*
* @param {String} msgid String to be translated

@@ -102,10 +108,10 @@ * @param {String} msgidPlural If no translation was found, return this on count!=1

*/
Gettext.prototype.ngettext = function(msgid, msgidPlural, count){
return this.dnpgettext(false, false, msgid, msgidPlural, count);
Gettext.prototype.ngettext = function(msgid, msgidPlural, count) {
return this.dnpgettext(this._currentDomain, '', msgid, msgidPlural, count);
};
/**
* Translates a plural string using a specific textdomain
*
* @param {String} domain Case insensitive language identifier
* Translates a plural string using a specific textdomain
*
* @param {String} domain Case insensitive language identifier
* @param {String} msgid String to be translated

@@ -116,4 +122,4 @@ * @param {String} msgidPlural If no translation was found, return this on count!=1

*/
Gettext.prototype.dngettext = function(domain, msgid, msgidPlural, count){
return this.dnpgettext(domain, false, msgid, msgidPlural, count);
Gettext.prototype.dngettext = function(domain, msgid, msgidPlural, count) {
return this.dnpgettext(domain, '', msgid, msgidPlural, count);
};

@@ -123,9 +129,9 @@

* Translates a string from a specific context using the default textdomain
*
* @param {String} context Translation context
*
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {String} translation or the original string if no translation was found
*/
Gettext.prototype.pgettext = function(context, msgid){
return this.dnpgettext(false, context, msgid);
Gettext.prototype.pgettext = function(msgctxt, msgid) {
return this.dnpgettext(this._currentDomain, msgctxt, msgid);
};

@@ -135,17 +141,16 @@

* Translates a string from a specific context using s specific textdomain
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
*
* @param {String} domain Case insensitive language identifier
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {String} translation or the original string if no translation was found
*/
Gettext.prototype.dpgettext = function(domain, context, msgid){
return this._getTranslation(domain, msgid, context) || msgid;
return this.dnpgettext(domain, context, msgid);
Gettext.prototype.dpgettext = function(domain, msgctxt, msgid) {
return this.dnpgettext(domain, msgctxt, msgid);
};
/**
* Translates a plural string from a specifi context using the default textdomain
*
* @param {String} context Translation context
* Translates a plural string from a specifi context using the default textdomain
*
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated

@@ -156,11 +161,11 @@ * @param {String} msgidPlural If no translation was found, return this on count!=1

*/
Gettext.prototype.npgettext = function(context, msgid, msgidPlural, count){
return this.dnpgettext(false, context, msgid, msgidPlural, count);
Gettext.prototype.npgettext = function(msgctxt, msgid, msgidPlural, count) {
return this.dnpgettext(this._currentDomain, msgctxt, msgid, msgidPlural, count);
};
/**
* Translates a plural string from a specifi context using a specific textdomain
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
* Translates a plural string from a specifi context using a specific textdomain
*
* @param {String} domain Case insensitive language identifier
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated

@@ -171,236 +176,93 @@ * @param {String} msgidPlural If no translation was found, return this on count!=1

*/
Gettext.prototype.dnpgettext = function(domain, context, msgid, msgidPlural, count){
Gettext.prototype.dnpgettext = function(domain, msgctxt, msgid, msgidPlural, count) {
var defaultTranslation = msgid;
if(!isNaN(count) && count != 1){
defaultTranslation = msgidPlural;
}
return this._getTranslation(domain, msgid, context, count) || defaultTranslation;
};
var translation;
var index;
/**
* Retrieves comments for a translation
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
* @param {String} msgid String to be translated
* @return {Object} comments for the translation in the form of {comment, note, code}
*/
Gettext.prototype.getComment = function(domain, context, msgid){
domain = (domain || this._textdomain || "").toLowerCase().trim();
if(this._domains[domain]){
return this._domains[domain].getComment(msgid, context);
}else{
return false;
domain = this._normalizeDomain(domain);
msgctxt = msgctxt || '';
if (!isNaN(count) && count !== 1) {
defaultTranslation = msgidPlural || msgid;
}
};
/**
* Sets a comment for a translation
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
* @param {String} msgid String to be translated
* @return {String|Object} comment for the translation in the form of {comment, note, code} or a string
*/
Gettext.prototype.setComment = function(domain, context, msgid, comment){
domain = (domain || this._textdomain || "").toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
textdomain.setComment(msgid, context, comment);
translation = this._getTranslation(domain, msgctxt, msgid);
if (translation) {
if (typeof count === 'number') {
index = this.domains[domain].pluralsFunc(count);
if (typeof index === 'boolean') {
index = index ? 1 : 0;
}
} else {
index = 0;
}
return translation.msgstr[index] || defaultTranslation;
}
return defaultTranslation;
};
/**
* Adds/replaces a translation in the translation table
*
* Retrieves comments object for a translation
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
* @param {String} original Original string
* @param {Array|String} translations Translations
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {Object} comments object or false if not found
*/
Gettext.prototype.setTranslation = function(domain, context, original, translations){
domain = (domain || "").toString().toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
textdomain.setTranslation(context, original, translations);
Gettext.prototype.getComment = function(domain, msgctxt, msgid) {
var translation;
domain = this._normalizeDomain(domain);
translation = this._getTranslation(domain, msgctxt, msgid);
if (translation) {
return translation.comments || {};
}
return {};
};
/**
* Removes a translation from the translation table
*
* Retrieves translation object from the domain and context
*
* @param {String} domain Case insensitive language identifier
* @param {String} context Translation context
* @param {String} original Original string
* @param {String} msgctxt Translation context
* @param {String} msgid String to be translated
* @return {Object} translation object or false if not found
*/
Gettext.prototype.deleteTranslation = function(domain, context, original){
domain = (domain || "").toString().toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
textdomain.setTranslation(context, original);
}
};
Gettext.prototype._getTranslation = function(domain, msgctxt, msgid) {
var translation;
/**
* List available context names
*
* @param {String} [domain] Case insensitive language identifier
* @return {Array} An array of context names
*/
Gettext.prototype.listContextNames = function(domain){
domain = (domain || "").toString().toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
return Object.keys(textdomain._translationTable).sort(function(a,b){
return a.localeCompare(b);
});
}
return [];
};
msgctxt = msgctxt || '';
domain = this._normalizeDomain(domain);
/**
* List available translation keys
*
* @param {String} [domain] Case insensitive language identifier
* @param {String} context Context name
* @return {Array} An array of translation keys
*/
Gettext.prototype.listKeys = function(domain, context){
domain = (domain || "").toString().toLowerCase().trim();
context = (context || "").toString();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
if(textdomain._translationTable[context]){
return Object.keys(textdomain._translationTable[context]).sort(function(a,b){
return a.localeCompare(b);
});
if (this.domains.hasOwnProperty(domain)) {
if (this.domains[domain].translations && this.domains[domain].translations[msgctxt]) {
if ((translation = this.domains[domain].translations[msgctxt][msgid])) {
return translation;
}
}
}
return [];
};
/**
* Compiles a language table into a .MO file
*
* @return {Buffer} Binary MO file contents, can be saved to disk etc.
*/
Gettext.prototype.compileMO = function(domain){
domain = (domain || "").toString().toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
return textdomain.compileMO();
}
return false;
};
/**
* Compiles a language table into a .PO file
*
* @return {Buffer} PO file contents, can be saved to disk etc.
* Normalizes textdomain value
*
* @param {String} domain Textdomain
* @param {Boolean} [isShort] If true then returns only language
* @returns {String} Normalized textdomain
*/
Gettext.prototype.compilePO = function(domain){
domain = (domain || "").toString().toLowerCase().trim();
var textdomain = this._domains[domain] || this._domains[this._textdomain] || false;
if(textdomain){
return textdomain.compilePO();
}
};
Gettext.prototype._normalizeDomain = function(domain, isShort) {
var parts = (domain || '').toString().split('.').shift().split(/[\-_]/);
var language = (parts.shift() || '').toLowerCase();
var locale = (parts.join('-') || '').toUpperCase();
/**
* Registers gettext functions to String.prototype for easier (and global) access
*/
Gettext.prototype.registerStringHelpers = function(){
var gt = this;
// textdomain
if(!("textdomain" in String.prototype)){
Object.defineProperty(String.prototype, "textdomain", {
value: function(domain){
return gt.textdomain(domain);
},
enumerable: false
});
if (isShort) {
return language;
} else {
return [].concat(language || []).concat(locale || []).join('_');
}
// gettext
if(!("gettext" in String.prototype)){
Object.defineProperty(String.prototype, "gettext", {
value: function(){
return gt.gettext(this.toString());
},
enumerable: false
});
}
// dgettext
if(!("dgettext" in String.prototype)){
Object.defineProperty(String.prototype, "dgettext", {
value: function(domain){
return gt.dgettext(domain, this.toString());
},
enumerable: false
});
}
// ngettext
if(!("ngettext" in String.prototype)){
Object.defineProperty(String.prototype, "ngettext", {
value: function(msgidPlural, count){
return gt.ngettext(this.toString(), msgidPlural, count);
},
enumerable: false
});
}
// dngettext
if(!("dngettext" in String.prototype)){
Object.defineProperty(String.prototype, "dngettext", {
value: function(domain, msgidPlural, count){
return gt.dngettext(domain, this.toString(), msgidPlural, count);
},
enumerable: false
});
}
// pgettext
if(!("pgettext" in String.prototype)){
Object.defineProperty(String.prototype, "pgettext", {
value: function(context){
return gt.pgettext(context, this.toString());
},
enumerable: false
});
}
// dpgettext
if(!("dpgettext" in String.prototype)){
Object.defineProperty(String.prototype, "dpgettext", {
value: function(domain, context){
return gt.dpgettext(domain, context, this.toString());
},
enumerable: false
});
}
// npgettext
if(!("npgettext" in String.prototype)){
Object.defineProperty(String.prototype, "npgettext", {
value: function(context, msgidPlural, count){
return gt.npgettext(context, this.toString(), msgidPlural, count);
},
enumerable: false
});
}
// dnpgettext
if(!("dnpgettext" in String.prototype)){
Object.defineProperty(String.prototype, "dnpgettext", {
value: function(domain, context, msgidPlural, count){
return gt.dnpgettext(domain, context, this.toString(), msgidPlural, count);
},
enumerable: false
});
}
};
};
{
"name": "node-gettext",
"description": "Gettext client for Node.js to use .mo files for I18N",
"version": "0.2.14",
"author" : "Andris Reinman",
"maintainers":[
{
"name":"andris",
"email":"andris@node.ee"
}
],
"homepage": "http://github.com/andris9/node-gettext",
"repository" : {
"type" : "git",
"url" : "http://github.com/andris9/node-gettext.git"
},
"scripts":{
"test": "nodeunit test/"
},
"main" : "./index",
"licenses" : [
{
"type": "MIT",
"url": "http://github.com/andris9/node-gettext/blob/master/LICENSE"
}
],
"dependencies": {
"iconv":">=1.2.0"
},
"devDependencies": {
"nodeunit": "*"
},
"engine": {
"node": ">=0.6"
},
"keywords": ["i18n", "l10n", "gettext", "mo"]
}
"name": "node-gettext",
"description": "Gettext client for Node.js to use .mo files for I18N",
"version": "1.0.0",
"author": "Andris Reinman",
"maintainers": [{
"name": "andris",
"email": "andris@node.ee"
}],
"homepage": "http://github.com/andris9/node-gettext",
"repository": {
"type": "git",
"url": "http://github.com/andris9/node-gettext.git"
},
"scripts": {
"test": "grunt"
},
"main": "./lib/gettext",
"licenses": [{
"type": "MIT",
"url": "http://github.com/andris9/node-gettext/blob/master/LICENSE"
}],
"dependencies": {
"gettext-parser": "^1.1.0"
},
"devDependencies": {
"chai": "^1.10.0",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.1.0"
},
"engine": {
"node": ">=0.6"
},
"keywords": [
"i18n",
"l10n",
"gettext",
"mo"
]
}

@@ -5,2 +5,4 @@ # node-gettext

**NB!** If you just want to parse or compile mo/po files, check out [gettext-parser](https://github.com/andris9/gettext-parser).
## Features

@@ -10,4 +12,2 @@

* Supports contexts and plurals
* Add your own translations to the list
* Recompile current translation table into a *MO* or a *PO* file!

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

var gt = new Gettext();
### Add a language

@@ -76,3 +76,3 @@

var greeting = gt.gettext("Hello!");
### Load a string from a specific language file

@@ -83,3 +83,3 @@

var greeting = gt.dgettext("et", "Hello!");
### Load a plural string from default language file

@@ -96,3 +96,3 @@

gt.dngettext("et", "%d Comment", "%d Comments", 10)
### Load a string of a specific context

@@ -109,3 +109,3 @@

gt.dpgettext("et", "menu items", "File");
### Load a plural string of a specific context

@@ -116,3 +116,3 @@

gt.npgettext("menu items", "%d Recent File", "%d Recent Files", 3);
### Load a plural string of a specific context from specific language file

@@ -130,84 +130,12 @@

Returns an object in the form of `{comment: "", code: "", note: ""}`
Returns an object in the form of `{translator: "", extracted: "", reference: "", flag: "", previous: ""}`
### Sets a comments for a translation
## Advanced handling
*setComment(domain, msgctxt, msgid, comment)*
If you need the translation object for a domain, for example `et_EE`, you can access it from `gt.domains.et_EE`.
gt.getComment("et", "menu items", "%d Recent File", "This is a comment");
If you want modify it and compile it to *mo* or *po*, checkout [gettext-parser](https://github.com/andris9/gettext-parser) module.
`comment` can either be a string or an object with the following properties: `{comment: "", code: "", note: ""}`.
## String helpers
In order to make things really easy, it is possible to attach the gettext functions directly to string
prototypes with `gettext.registerStringHelpers()`
Example:
// setup gettext
var gettext = new Gettext();
gettext.registerStringHelpers();
gettext.addTextdomain("et", fs.readFileSync("et.mo"));
// translate any string
var translated = "translate this string".gettext();
// or
var plural = "translate %s string".ngettext("translate %s strings", 10);
// you can even change the default textdomain
"".textdomain("en");
The parameters for the gettext functions are the same as with regular gettext methods, except that the `msgid` parameter is not needed.
## Manage translations
### Add a translation
*setTranslation(domain, context, msgid, translation)*
gt.setTranslation("et", "", "Hello", "Tere");
Use an array for plurals
gt.setTranslation("et", "", "%s comments", ["%s kommentaar", "%s kommentaari"]);
### Remove a translation
*deleteTranslation(domain, context, msgid)*
gt.deleteTranslation("et", "", "Hello");
### List available contexts
*listContextNames([domain])*
var contextStrArr = gt.listContextNames("et");
### List translation keys for a context
*listKeys([domain], [context])*
var originalsStrArr = gt.listKeys("et", "");
## Compiling
### Compile to MO
Compile current translation table to a *MO* file
*compileMO([domain])*
fs.writeFile("out.mo", gt.compileMO("et"));
### Compile to PO
Compile current translation table to a *PO* file
*compilePO([domain])*
fs.writeFile("out.po", gt.compilePO("et"));
## License
MIT
MIT

Sorry, the diff of this file is not supported yet

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