Permalink
Browse files

.

  • Loading branch information...
1 parent cb6c709 commit 595492278957b15530f1a5e34d262296808840e9 @masylum committed Oct 31, 2010
Showing with 50 additions and 0 deletions.
  1. 0 README
  2. +11 −0 Readme.md
  3. +1 −0 index.js
  4. +38 −0 lib/connect-i18n.js
View
0 README
No changes.
View
@@ -0,0 +1,11 @@
+# Connect i18n middleware
+
+Almost inexistant middleware that checks the accept-languages
+header and adds them to the request.
+
+ npm install 'connect-i18n'
+
+ var i18n = require('connect-i18n');
+ app.use(i18n({default_locale: 'es-es'}));
+
+ console.log(req.locales);
View
@@ -0,0 +1 @@
+module.exports = require('./lib/connect-i18n');
View
@@ -0,0 +1,38 @@
+/**
+ * Module dependencies.
+ */
+
+var queryString = require('querystring');
+
+/**
+ * Extracts locales from accept language headers
+ *
+ * @return {Function}
+ * @api public
+ */
+
+module.exports = function i18n(options) {
+
+ options = options || {};
+
+ if (!options.default_locale) {
+ options.default_locale = 'en_us';
+ }
+
+ return function i18n(req, res, next) {
+ var acceptLanguage = req.headers['accept-language'],
+ locales = [];
+
+ if (acceptLanguage) {
+ acceptLanguage.split(',').forEach(function (lang) {
+ locales.push(lang.split(';', 1)[0].toLowerCase());
+ });
+
+ req.locales = locales;
+ } else {
+ req.locales = [options.default_locale];
+ }
+
+ next();
+ };
+};

0 comments on commit 5954922

Please sign in to comment.