title: localization nodejs (i18n)
body: <p>My code: </p>
  
  <pre><code>var i18n = require("i18n");
  
  i18n.configure({
      locales: ['en', 'ru'],
      defaultLocale: 'en',
      directory: __dirname + '/locales',
      cookiename: 'locale'
  });
  
  app.configure(function () {
      app.use(i18n.init);
  })
  
  app.get('/:locale', function (req, res) { 
      res.cookie('locale', req.params.locale); 
      i18n.setLocale(req.params.locale); 
      res.redirect('/'); 
  });
  </code></pre>
  
  <p>The problems are two:</p>
  
  <ol>
  <li><p>In template does not work output through</p>
  
  <p>__("Name Key")</p></li>
  <li><p>When the transfer is not directly through value, do not change text. There are all languages files </p>
  
  <p>res.render('index', {name: res.__('name') });</p></li>
  <li><p>But do not switch languages​​</p></li>
  </ol>
  
  <p>/* ----- */</p>
  
  <p>The resulting code: </p>
  
  <pre><code>var i18n = require("i18n");
  
  i18n.configure({
      locales: ['en', 'ru'],
      defaultLocale: 'ru',
      directory: __dirname + '/locales'//,
      cookiename: 'locale'
  });
  
  
  app.use(function (req, res, next) {
      res.locals.__ = res.__ = function() {
          return i18n.__.apply(req, arguments);
      };
      next();
  });
  
  app.get('/i18n/:locale', function (req, res) {
      res.cookie('locale', req.params.locale);
      i18n.setLocale(req.params.locale);
      if (req.headers.referer) res.redirect(req.headers.referer);
      else res.redirect("/");
  });
  </code></pre>
  
  <p>This works</p>