commonplace
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -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" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
119595
21
1170
3
1
12
+ Addedlodash@^3.10.0
+ Addedlodash@3.10.1(transitive)