Permalink
Browse files

Adding support for Express 3

  • Loading branch information...
ozten committed Dec 14, 2012
1 parent c052f76 commit 0d882ad7bf61438ee6e5978da04d571daf7ae00e
@@ -0,0 +1 @@
+This example demonstrates how to use i18n-abide with Express 2.
@@ -1,10 +1,13 @@
{
- "name": "application-name",
- "version": "0.0.1",
- "private": true,
- "dependencies": {
- "express": "2.5.8",
- "ejs": ">= 0.0.1",
- "i18n-abide": ">= 0.0.7"
+ "name": "application-name",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "start": "node app"
+ },
+ "dependencies": {
+ "express": "2.5.8",
+ "ejs": "*",
+ "i18n-abide": ">= 0.0.7"
}
}
@@ -1,2 +0,0 @@
-<h1><%= title %></h1>
-<p>Welcome to <%= title %></p>
View
@@ -0,0 +1,42 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express'),
+ i18n = require('i18n-abide'),
+ routes = require('./routes'),
+ user = require('./routes/user'),
+ http = require('http'),
+ path = require('path');
+
+var app = express();
+
+app.configure(function(){
+ app.set('port', process.env.PORT || 3000);
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'ejs');
+ app.use(i18n.abide({
+ supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
+ default_lang: 'en-US',
+ debug_lang: 'it-CH',
+ locale_directory: 'locale'
+ }));
+ app.use(express.favicon());
+ app.use(express.logger('dev'));
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(path.join(__dirname, 'public')));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler());
+});
+
+app.get('/', routes.index);
+app.get('/users', user.list);
+
+http.createServer(app).listen(app.get('port'), function(){
+ console.log("Express server listening on port " + app.get('port'));
+});
Binary file not shown.
@@ -0,0 +1,22 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-24 09:50+0200\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: /home/ozten/i18n-abide/examples/express2/views/index.ejs:1
+msgid "Hello, world!"
+msgstr "‮Hǝʅʅo´ ʍoɹʅp¡"
Binary file not shown.
@@ -0,0 +1,22 @@
+# German translations for express package.
+# Copyright (C) 2012 THE express'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the express package.
+# Austin King <shout@ozten.com>, 2012.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: express 2\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-24 09:50+0200\n"
+"PO-Revision-Date: 2012-12-14 10:48-0800\n"
+"Last-Translator: Austin King <shout@ozten.com>\n"
+"Language-Team: German\n"
+"Language: de\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: /home/ozten/i18n-abide/examples/express2/views/index.ejs:1
+msgid "Hello, world!"
+msgstr "Hallo Welt"
Binary file not shown.
@@ -0,0 +1,22 @@
+# English translations for express package.
+# Copyright (C) 2012 THE express'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the express package.
+# Austin King <shout@ozten.com>, 2012.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: express 2\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-24 09:50+0200\n"
+"PO-Revision-Date: 2012-12-14 10:49-0800\n"
+"Last-Translator: Austin King <shout@ozten.com>\n"
+"Language-Team: English\n"
+"Language: en_US\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: /home/ozten/i18n-abide/examples/express2/views/index.ejs:1
+msgid "Hello, world!"
+msgstr "Hello, world!"
Binary file not shown.
@@ -0,0 +1,22 @@
+# Spanish translations for express package.
+# Copyright (C) 2012 THE express'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the express package.
+# Austin King <shout@ozten.com>, 2012.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: express 2\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-24 09:50+0200\n"
+"PO-Revision-Date: 2012-12-14 10:49-0800\n"
+"Last-Translator: Austin King <shout@ozten.com>\n"
+"Language-Team: Spanish\n"
+"Language: es\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: /home/ozten/i18n-abide/examples/express2/views/index.ejs:1
+msgid "Hello, world!"
+msgstr "¡Hola mundo!"
@@ -0,0 +1,23 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-24 09:50+0200\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: /home/ozten/i18n-abide/examples/express2/views/index.ejs:1
+msgid "Hello, world!"
+msgstr ""
+
@@ -0,0 +1,13 @@
+{
+ "name": "application-name",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "start": "node app"
+ },
+ "dependencies": {
+ "express": "3.0.4",
+ "ejs": "*",
+ "i18n-abide": ">= 0.0.7"
+ }
+}
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
@@ -0,0 +1,8 @@
+
+/*
+ * GET home page.
+ */
+
+exports.index = function(req, res){
+ res.render('index', { title: 'Express' });
+};
@@ -0,0 +1,8 @@
+
+/*
+ * GET users listing.
+ */
+
+exports.list = function(req, res){
+ res.send("respond with a resource");
+};
@@ -1,10 +1,10 @@
<!DOCTYPE html>
-<html>
+<html lang="<%= lang %>" dir="<%= lang_dir %>">
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
- <%- body %>
+ <h1><%= gettext("Hello, world!") %></h1>
</body>
-</html>
+</html>
View
@@ -98,24 +98,35 @@ exports.abide = function (options) {
if (lang && lang.toLowerCase && lang.toLowerCase() == debug_lang) {
lang = 'db-LB'; // What? http://www.youtube.com/watch?v=rJLnGjhPT1Q
}
+ // Express 2 support
+ if (!! resp.local) {
+ resp.locals = function (args, orValue) {
+ if ('string' === typeof args) {
+ resp.local(args, orValue);
+ } else {
+ Object.keys(args).forEach(function (key, i) {
+ resp.local(key, args[key]);
+ });
+ }
+ };
+ }
- resp.locals('lang', lang);
+ var locals = {};
+ locals.lang = lang;
// BIDI support, which direction does text flow?
lang_dir = BIDI_RTL_LANGS.indexOf(lang) >= 0 ? 'rtl' : 'ltr';
- resp.locals('lang_dir', lang_dir);
+ locals.lang_dir = lang_dir;
req.lang = lang;
locale = localeFrom(lang);
- resp.locals('locale', locale);
+ locals.locale = locale;
req.locale = locale;
- resp.locals('format', format);
+ locals.format = format;
req.format = format;
- resp.locals('format', format);
-
if (mo_cache[locale].mo_exists) {
if (mo_cache[locale].gt === null) {
mo_cache[locale].gt = new Gettext();
@@ -125,21 +136,25 @@ exports.abide = function (options) {
mo_cache[locale].gt.textdomain(locale);
}
var gt = mo_cache[locale].gt;
- resp.locals(options.gettext_alias, gt.gettext.bind(gt));
+ locals[options.gettext_alias] = gt.gettext.bind(gt);
req.gettext = gt.gettext.bind(gt);
- resp.locals(options.ngettext_alias, gt.ngettext.bind(gt));
+ locals[options.ngettext_alias] = gt.ngettext.bind(gt);
req.ngettext = gt.ngettext.bind(gt);
- } else {
+ } else {
// en-US in a non gettext environment... fake it
var identity = function (a, b) { return a; };
- resp.locals(options.gettext_alias, identity);
+ locals[options.gettext_alias] = identity;
req.gettext = identity;
- resp.locals(options.ngettext_alias, identity);
+ locals[options.ngettext_alias] = identity;
req.ngettext = identity;
}
+
+ // resp.locals(string, value) doesn't seem to work with EJS
+ resp.locals(locals);
next();
};
};
+
function qualityCmp(a, b) {
if (a.quality == b.quality) {
return 0;

0 comments on commit 0d882ad

Please sign in to comment.