Skip to content

Instantly share code, notes, and snippets.

@mashpie
Last active May 1, 2024 19:55

Revisions

  1. mashpie revised this gist Apr 28, 2013. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions i18n-express-hbs-app.js
    Original file line number Diff line number Diff line change
    @@ -22,11 +22,6 @@ app.configure(function () {
    // init i18n module for this loop
    app.use(i18n.init);

    // register locale to res.locals so hbs helpers know this.locale
    app.use(function (req, res, next) {
    res.locals.locale = req.locale;
    next();
    });
    });

    // register hbs helpers in res.locals' context which provides this.locale
  2. mashpie revised this gist Mar 26, 2013. 1 changed file with 1 addition and 14 deletions.
    15 changes: 1 addition & 14 deletions i18n-express-hbs-app.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    // require modules
    var express = require('express'),
    i18n = require('i18n'),
    url = require('url'),
    hbs = require('hbs'),
    app = module.exports = express();

    @@ -38,22 +37,10 @@ hbs.registerHelper('__n', function () {
    return i18n.__n.apply(this, arguments);
    });

    // delay a response to simulate a long running process,
    // while another request comes in with altered language settings
    // serving homepage
    app.get('/', function (req, res) {
    res.render('index');
    });

    // set a cookie to requested locale
    app.get('/:locale', function (req, res) {
    res.cookie('locale', req.params.locale);
    res.redirect("/?delay=" + app.getDelay(req, res));
    });

    // simple param parsing
    app.getDelay = function (req, res) {
    return url.parse(req.url, true).query.delay || 0;
    };

    // startup
    app.listen(3000);
  3. mashpie revised this gist Mar 26, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion i18n-express-hbs-app.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,6 @@ app.configure(function () {
    res.locals.locale = req.locale;
    next();
    });
    app.use(app.router);
    });

    // register hbs helpers in res.locals' context which provides this.locale
  4. mashpie revised this gist Mar 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion i18n-express-hbs-app.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // require modules
    var express = require('express'),
    i18n = require('../../i18n'),
    i18n = require('i18n'),
    url = require('url'),
    hbs = require('hbs'),
    app = module.exports = express();
  5. mashpie created this gist Mar 26, 2013.
    60 changes: 60 additions & 0 deletions i18n-express-hbs-app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    // require modules
    var express = require('express'),
    i18n = require('../../i18n'),
    url = require('url'),
    hbs = require('hbs'),
    app = module.exports = express();

    i18n.configure({
    locales: ['en', 'fr'],
    cookie: 'locale',
    directory: "" + __dirname + "/locales"
    });

    app.configure(function () {
    // setup hbs
    app.set('views', "" + __dirname + "/views");
    app.set('view engine', 'hbs');
    app.engine('hbs', hbs.__express);

    // you'll need cookies
    app.use(express.cookieParser());

    // init i18n module for this loop
    app.use(i18n.init);

    // register locale to res.locals so hbs helpers know this.locale
    app.use(function (req, res, next) {
    res.locals.locale = req.locale;
    next();
    });
    app.use(app.router);
    });

    // register hbs helpers in res.locals' context which provides this.locale
    hbs.registerHelper('__', function () {
    return i18n.__.apply(this, arguments);
    });
    hbs.registerHelper('__n', function () {
    return i18n.__n.apply(this, arguments);
    });

    // delay a response to simulate a long running process,
    // while another request comes in with altered language settings
    app.get('/', function (req, res) {
    res.render('index');
    });

    // set a cookie to requested locale
    app.get('/:locale', function (req, res) {
    res.cookie('locale', req.params.locale);
    res.redirect("/?delay=" + app.getDelay(req, res));
    });

    // simple param parsing
    app.getDelay = function (req, res) {
    return url.parse(req.url, true).query.delay || 0;
    };

    // startup
    app.listen(3000);
    5 changes: 5 additions & 0 deletions index.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <span id="text">{{{__ "text to test"}}}</span>
    <br>
    <span id="onecat">{{{__n "%d cat" "%d cats" 1}}}</span>
    <br>
    <span id="twocats">{{{__n "%d cat" "%d cats" 2}}}</span>