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

commonplace

Package Overview
Dependencies
Maintainers
8
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commonplace - npm Package Compare versions

Comparing version 2.4.0 to 2.5.0

messages.po

105

lib/generate_langpacks.js

@@ -0,5 +1,19 @@

/*
Parses .po file, outputting a JS langpack.
Will output {'My String': 'El String'} for simple non-plural localizations.
Will output {'Result': ['El Resulto', 'Los Resultos']} for plural
localizations, where the first index is the singular localized string
and the second index is the plural localized string.
Will not output strings that don't contain a localization. The l10n
library should simply output the ID in that case.
Will not output strings where the localization is equivalent to the ID.
*/
var fs = require('fs');
var util = require('util');
var _ = require('lodash');
function sw(x, y) {return x.substr(0, y.length) === y;}
var S_ID = 'msgid ';

@@ -12,2 +26,11 @@ var S_STR = 'msgstr ';

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
function parse(po_content) {

@@ -20,14 +43,34 @@ var output = {};

var id = null;
var pluralId = null;
var last = null;
var last_plural = null;
var current_obj = {body: ''};
var currentLocalizedResult = '';
function store_current() {
if (id) {
// Don't save a copy of the headers in the langpack.
output[id] = current_obj;
var hasResult = !!currentLocalizedResult;
var hasResultPlural = currentLocalizedResult.constructor === String ||
_.filter(currentLocalizedResult).length > 0;
// Don't set strings that are identical to their keys.
var notIdentical = id !== currentLocalizedResult;
var notIdenticalPlural = currentLocalizedResult.constructor === String ||
currentLocalizedResult.length > 2 ||
currentLocalizedResult[0] !== id &&
currentLocalizedResult[1] !== pluralId;
if (currentLocalizedResult.constructor === Array &&
currentLocalizedResult.length === 1) {
currentLocalizedResult = currentLocalizedResult[0];
}
if (hasResult && hasResultPlural && notIdentical &&
notIdenticalPlural) {
// Set the string in our langpack if all conditions pass.
output[id] = currentLocalizedResult;
}
} else {
// If there's no IDs, it's probably the headers. If there's a
// pluralizer up there, use it.
var parsed_headers = current_obj.body.split('\n');
var parsed_headers = currentLocalizedResult.split('\n');
parsed_headers.forEach(function(v) {

@@ -40,3 +83,3 @@ var plex_match = RE_PLURALIZER.exec(v);

id = '';
current_obj = {body: ''};
currentLocalizedResult = null;
}

@@ -59,3 +102,3 @@

if (sw(line, S_ID)) {
if (line.startsWith(S_ID)) {
// console.log('...Storing existing id: ', id);

@@ -69,8 +112,8 @@ store_current();

}
if (sw(line, S_PLURAL_STR)) {
if (line.startsWith(S_PLURAL_STR)) {
// pl_match[0] is the index (0 for singular, 1 for plural).
// pl_match[2] is the matched string.
var pl_match = RE_PLURAL.exec(line);
if (!('plurals' in current_obj)) {
current_obj.plurals = [];
}
current_obj.plurals[pl_match[1]] = JSON.parse(pl_match[2]);
currentLocalizedResult = currentLocalizedResult || [];
currentLocalizedResult.push(JSON.parse(pl_match[2]));
last = 'plurals';

@@ -80,17 +123,13 @@ last_plural = pl_match[1];

}
if (sw(line, S_STR)) {
if (line.startsWith(S_STR)) {
last = 'body';
var body = JSON.parse(line.substr(S_STR.length));
// console.log(' > Storing body: ', body);
current_obj.body += body;
currentLocalizedResult = body;
continue;
}
if (sw(line, S_PLURAL)) {
if (line.startsWith(S_PLURAL)) {
last = 'plural';
var plural = JSON.parse(line.substr(S_PLURAL.length));
// console.log(' > Plural form: ', plural);
if (!('plural' in current_obj)) {
current_obj.plural = '';
}
current_obj.plural += plural;
pluralId = JSON.parse(line.substr(S_PLURAL.length));
currentLocalizedResult = currentLocalizedResult || [];
continue;

@@ -102,3 +141,3 @@ }

// console.log(' >> Appending plural: ', line_val);
current_obj.plurals[last_plural] += line_val;
currentLocalizedResult.plurals.push(line_val);
} else if (last === 'id') {

@@ -109,3 +148,3 @@ // console.log(' >> Last was ID');

// console.log(' >> (' + last + ':' + id + ') Appending : ', line_val);
current_obj[last] += line_val;
currentLocalizedResult += line_val;
}

@@ -146,10 +185,14 @@ }

var compiled = gen_pack(data, lang);
fs.writeFile(dest_path, compiled, function(err) {
if (err) {
console.error('Unable to write language pack to: ' + dest_path, err);
}
if (callback) {
callback(err);
}
});
if (dest_path) {
fs.writeFile(dest_path, compiled, function(err) {
if (err) {
console.error('Unable to write language pack to: ' + dest_path, err);
}
if (callback) {
callback(err);
}
});
} else {
callback(null, compiled);
}
});

@@ -156,0 +199,0 @@ }

{
"name": "commonplace",
"description": "Reusable components for Firefox Marketplace frontend projects.",
"version": "2.4.0",
"version": "2.5.0",
"main": "lib/commonplace",
"preferGlobal": true,
"scripts": {
"test": "mocha test/tests.js"
},
"repository": {

@@ -17,2 +20,3 @@ "url": "git://github.com/mozilla/commonplace.git",

"jshint": "2.3.x",
"lodash": "^3.10.0",
"nunjucks": "1.0.x"

@@ -25,3 +29,6 @@ },

"boss": true
},
"devDependencies": {
"mocha": "^2.2.5"
}
}
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