express-guess-lang
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -10,31 +10,30 @@ var egl = exports | ||
regions = [], | ||
langs = [], | ||
likely = { lang: 'en', likelihood: 0.0} | ||
languages = [], | ||
lookup = {}, | ||
likely = { name: 'en', likelihood: 0.0 } | ||
if (typeof accept_lang === 'string') { | ||
accept_lang = accept_lang.replace(/\s/g, '') | ||
var candidates = accept_lang.split(',') | ||
for (var i = 0; i < candidates.length; i++) { | ||
var match = candidates[i].match(/q=([0-9].[0-9])/) | ||
var likelihood = match ? match[1] : 0.0 | ||
match = candidates[i].match(/-([^;]*)/) | ||
if (match) | ||
regions.push(match[1]) | ||
match = candidates[i].match(/(.*)[;||-]/) | ||
var lang = match ? match[1] : '' | ||
var newLang = { lang: lang, likelihood: likelihood } | ||
var matches = candidates[i].match(/([^-;]*)(?:-([^;]*))?(?:;q=([0-9]\.[0-9]))?/) | ||
if (matches[2]) | ||
regions.push(matches[2]) | ||
var newLang = { name: matches[1], likelihood: matches[3] ? matches[3] : 1.0 } | ||
languages.push(newLang) | ||
lookup[newLang.name] = newLang.likelihood | ||
if (newLang.likelihood > likely.likelihood) | ||
likely = newLang | ||
if (likelihood > 0.0) | ||
langs.push(newLang) | ||
} | ||
request.getRegions = function () { return regions } | ||
request.getLanguages = function () { return langs } | ||
request.getLikelyLanguage = function () { return likely.lang } | ||
} | ||
else { | ||
request.getRegions = function () { return [ defaultRegion ] } | ||
request.getLanguages = function () { return [{ lang: defaultLang, likelihood: 0.0 }] } | ||
request.getLikelyLanguage = function () { return defaultLang } | ||
likely = { name: defaultLang, likelihood: 1.0 } | ||
regions = [ defaultRegion ] | ||
languages = [ likely ] | ||
} | ||
request.getRegions = function () { return regions } | ||
request.getLanguages = function () { return languages } | ||
request.getLikelyLanguage = function () { return likely.name } | ||
request.isAppropriate = function (name) { return (typeof lookup[name] !== 'undefined') } | ||
if (typeof next === 'function') | ||
@@ -41,0 +40,0 @@ next() |
{ | ||
"name": "express-guess-lang", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Provides the language accepted by the users browser in the request object", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -53,2 +53,3 @@ # express-guess-lang | ||
var preferedLanguage = request.getLikelyLanguage() | ||
var acceptable = request.isAppropriate('en') | ||
}) | ||
@@ -73,2 +74,7 @@ | ||
Check if a language is appropriate for the user: | ||
isAppropriate('en'): boolean | ||
//Example: true | ||
## Licensed under MIT | ||
@@ -75,0 +81,0 @@ |
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
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
5363
98
36