i18n-abide
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -50,3 +50,3 @@ # API | ||
The following example shows how to set the locale of the request to `zh_TW` | ||
(Traditional Chinese): | ||
(Traditional Chinese): | ||
@@ -76,2 +76,20 @@ exports.homepage = function(req, resp) { | ||
For example, the `es` language listed above should be located at | ||
`i18n/es/messages.plist`. | ||
`i18n/es/messages.plist`. | ||
## format_fn_name Option | ||
For compatibility with `express-resource` and other apps, | ||
you may use the `format_fn_name` option to rename the `format` function. | ||
Example: | ||
app.use(i18n.abide({format_fn_name: 'i18nformat'})); | ||
app.get('/foo', function(req, res) { | ||
res.render('bar', { | ||
greeting: req.i18nformat("%s %s!", ["Hello", "World"]); | ||
}); | ||
}); | ||
# in bar.ejs | ||
<p><%= i18nformat(gettext("Please see %s"), ["https://example.com"]) %></p> |
@@ -62,3 +62,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
// We expect to use PO->json files, unless configured to use another format. | ||
options.translation_type = options.translation_type || 'po'; | ||
options.translation_type = options.translation_type || 'po'; | ||
// Only check URL for locale if told to do so. | ||
@@ -190,5 +190,18 @@ options.locale_on_url = options.locale_on_url === true; | ||
locals.format = format; | ||
req.format = format; | ||
var formatFnName = 'format'; | ||
if (!! locals.format || !! req.format) { | ||
if (!! options.format_fn_name) { | ||
formatFnName = options.format_fn_name; | ||
} else { | ||
console.error("It appears you are using middleware which " + | ||
"already sets a variable 'format' on either the request " + | ||
"or reponse. Please use format_fn_name in options to " + | ||
"override this setting."); | ||
throw new Error("Bad Config - override format_fn_name"); | ||
} | ||
} | ||
locals[formatFnName] = format; | ||
req[formatFnName] = format; | ||
locals.setLocale = function(assignedLocale) { | ||
@@ -195,0 +208,0 @@ if (translations[assignedLocale]) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Express/connect module for Node i18n and l10n support", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"homepage": "https://github.com/mozilla/i18n-abide", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -163,4 +163,134 @@ #!/usr/bin/env node | ||
var makeResp = function(_locals) { | ||
return { | ||
locals: function(args, orValue) { | ||
if ('string' === typeof args) { | ||
_locals[args] = orValue; | ||
} else { | ||
Object.keys(args).forEach(function(key) { | ||
_locals[key] = args[key]; | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
suite.addBatch({ | ||
"i18n.abide middleware is setup": { | ||
topic: function(){ | ||
var middleware = i18n.abide({}); | ||
var that = this; | ||
var _locals = {}; | ||
var req = { | ||
headers: { | ||
'accept-language': "pl,fr-FR;q=0.3,en-US;q=0.1" | ||
} | ||
}; | ||
middleware(req, makeResp(_locals), function() { | ||
// The request and response objects both get | ||
// references to i18n related variables and fn | ||
// Example: req.lang as well as _locals.lang | ||
[req, _locals].forEach(function(obj){ | ||
assert.equal(obj.lang, 'en-US'); | ||
assert.equal(obj.locale, 'en_US'); | ||
assert.ok(obj.format); | ||
assert.equal(typeof obj.format, 'function'); | ||
assert.ok(obj.setLocale); | ||
assert.equal(typeof obj.setLocale, 'function'); | ||
assert.ok(obj.gettext); | ||
assert.equal(typeof obj.gettext, 'function'); | ||
}); | ||
that.callback(); | ||
}); | ||
}, | ||
'gets a callback': function(err) { | ||
assert.ok(! err); | ||
} | ||
} | ||
}); | ||
suite.addBatch({ | ||
"i18n.abide middleware detects format conflict": { | ||
topic: function(){ | ||
var middleware = i18n.abide({}); | ||
var that = this; | ||
var _locals = {}; | ||
var req = { | ||
headers: { | ||
'accept-language': "pl,fr-FR;q=0.3,en-US;q=0.1" | ||
}, | ||
// format from express-resource | ||
format: function() { | ||
} | ||
}; | ||
_locals.format = req.format; | ||
try { | ||
console.log('== calling middleware'); | ||
middleware(req, makeResp(_locals), function() { | ||
console.log('== calling assert fail'); | ||
that.callback(new Error('We should have failed')); | ||
}); | ||
} catch (e) { | ||
this.callback(); | ||
} | ||
}, | ||
'gets a callback': function(err) { | ||
assert.ok(! err); | ||
} | ||
} | ||
}); | ||
suite.addBatch({ | ||
"i18n.abide middleware allows re-naming the format fn": { | ||
topic: function(){ | ||
var middleware = i18n.abide({ | ||
format_fn_name: 'i18nFormat' | ||
}); | ||
var that = this; | ||
var _locals = {}; | ||
var req = { | ||
headers: { | ||
'accept-language': "pl,fr-FR;q=0.3,en-US;q=0.1" | ||
}, | ||
// format from express-resource | ||
format: function(v) { | ||
this.formatCalled = v; | ||
} | ||
}; | ||
_locals.format = req.format; | ||
middleware(req, makeResp(_locals), function() { | ||
// The request and response objects both get | ||
// references to i18n related variables and fn | ||
// Example: req.lang as well as _locals.lang | ||
[req, _locals].forEach(function(obj){ | ||
assert.ok(obj.format); | ||
assert.equal(typeof obj.format, 'function'); | ||
// Existing format is callable and functional | ||
obj.format('foo'); | ||
assert.ok(obj.formatCalled, 'foo'); | ||
// Our renamed i18n.format is callable and functional | ||
assert.ok(obj.i18nFormat); | ||
assert.equal(typeof obj.i18nFormat, 'function'); | ||
var h = obj.i18nFormat("%s %s!", ["Hello", "World"]); | ||
assert.equal(h, 'Hello World!'); | ||
}); | ||
that.callback(); | ||
}); | ||
}, | ||
'gets a callback': function(err) { | ||
assert.ok(! err); | ||
} | ||
} | ||
}); | ||
// run or export the suite. | ||
if (process.argv[1] === __filename) suite.run(); | ||
else suite.export(module); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances 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 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
1
9
2
68274
53
1108