Permalink
Browse files

Version 0.0.3 from my local repo now alive on github.

  • Loading branch information...
1 parent 6e7adce commit ad7fa9293d09646dba54127c5c3dd3622fae892d @akoenig committed Sep 25, 2011
View
@@ -0,0 +1,45 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express'),
+ lingua = require('lingua');
+
+var app = module.exports = express.createServer();
+
+// Configuration
+
+app.configure(function(){
+ app.register(".html", require("jqtpl").express);
+ app.set('views', __dirname + '/views');
+ app.set("view engine", "html");
+
+ // Lingua configuration
+ app.use(lingua(app, {
+ defaultLocale: 'de-de',
+ path: __dirname + '/i18n'
+ }));
+
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+
+app.get('/', function(req, res){
+ res.render('index');
+});
+
+app.listen(3000);
+console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
@@ -0,0 +1,7 @@
+{
+ "title": "express-lingua",
+ "index": {
+ "headline": "Eine Überschrift",
+ "description": "Dies ist eine Beispiel-Anwendung."
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "title": "express-lingua",
+ "index": {
+ "headline": "A headline",
+ "description": "This is an example application."
+ }
+}
View
@@ -0,0 +1,9 @@
+{
+ "name": "express lingua example app"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.4.6"
+ , "jqtpl": ">= 1.0.0"
+ }
+}
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
View
@@ -0,0 +1,2 @@
+<h1>${lingua.index.headline}</h1>
+<p>${lingua.index.description}</p>
View
@@ -0,0 +1,11 @@
+<!doctype html>
+<html>
+ <head>
+ <title>${lingua.title}</title>
+
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
+ </head>
+ <body>
+ {{html body}}
+ </body>
+</html>
View
@@ -0,0 +1,187 @@
+/*
+ * express-lingua
+ *
+ * Licensed under the MIT:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * Copyright (c) 2011, André König (andre.koenig -[at]- gmail [*dot*] com)
+ *
+ */
+var fs = require('fs');
+
+module.exports = function(app, options) {
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ var DEFAULTS = {
+ extension: '.json'
+ };
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ var resources = [];
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ var guru = {
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ learn : function() {
+ var path = options.path;
+
+ var files = fs.readdirSync(path);
+
+ files.forEach(function(file) {
+ var content = fs.readFileSync(path + file);
+
+ var resource = {
+ locale: file.replace(DEFAULTS.extension, ''),
+ content: JSON.parse(content)
+ };
+
+ resources.push(resource);
+ });
+
+ // Check if the resource for the default
+ // locale is available. If not, well, ERROR!
+ var available = false;
+ resources.forEach(function(resource) {
+ if (resource.locale === options.defaultLocale) {
+ available = true;
+ }
+ });
+
+ if (!available) {
+ throw new Error('lingua: Please create a resource file for your default locale: '+options.defaultLocale);
+ }
+ },
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ ask : function(locale) {
+ var resource = resources.filter(function(resource) {
+ return (resource.locale === locale);
+ })[0];
+
+ if (!resource) {
+ resource = resources.filter(function(resource) {
+ return (resource.locale === options.defaultLocale);
+ })[0];
+ }
+
+ return resource;
+ },
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ // note:
+ // connect-i18n: https://github.com/masylum/connect-i18n/blob/master/lib/connect-i18n.js
+ //
+ determine : function(headers) {
+ var accept_language = headers['accept-language'];
+ var tokens = [];
+ var locales = [];
+
+ var result = null;
+
+ if (accept_language) {
+ accept_language.split(',').forEach(function (lang) {
+ locales.push(lang.split(';', 1)[0].toLowerCase());
+ });
+
+ result = locales;
+ } else {
+ result = [options.defaultLocale];
+ }
+
+ return result[0];
+ },
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ help : function() {
+ app.dynamicHelpers({
+ lingua: function(req, res){
+ return res.lingua.content;
+ }
+ });
+ }
+ };
+
+ //
+ // Verify the given parameters.
+ //
+ if (!options.defaultLocale) {
+ throw new Error('lingua: Please define a default locale while registering the middleware.');
+ }
+
+ if (!options.path) {
+ throw new Error('lingua: Please define a path where lingua can find your locales.');
+ } else {
+ if (options.path[options.path.length] !== '/') {
+ options.path = options.path + '/';
+ }
+ }
+
+ //
+ // DOCME
+ //
+ guru.learn();
+
+ //
+ // DOCME
+ //
+ guru.help();
+
+ //
+ // summary:
+ // DOCME
+ //
+ // description:
+ // DOCME
+ //
+ return function lingua(req, res, next) {
+ var headers = req.headers;
+ var locale = guru.determine(headers);
+
+ res.lingua = guru.ask(locale);
+
+ next();
+ };
+};
View
@@ -0,0 +1,20 @@
+{
+ "name": "lingua",
+ "description": "A i18n middleware for the express.js framework.",
+ "version": "0.0.3",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/akoenig/express-lingua.git"
+ },
+ "author": "André König <andre.koenig@gmail.com> (http://lochkartenstanzer.de)",
+ "main": "./lib/lingua.js",
+ "directories": {
+ "lib": "lib"
+ },
+ "dependencies": {
+ "express": ">=2.3.10"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ }
+}

0 comments on commit ad7fa92

Please sign in to comment.