Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend for multilingual layouts and templates #14

Closed
rottmann opened this issue Mar 16, 2013 · 3 comments
Closed

Extend for multilingual layouts and templates #14

rottmann opened this issue Mar 16, 2013 · 3 comments

Comments

@rottmann
Copy link

My view/layout structure is:

views
|- de
   |- layouts
      |- main_layout.html
      |- main_layout.txt
   |- user
      |- register.html
      |- register.txt
|- en
   |- layouts
      |- main_layout.html
      |- main_layout.txt
   |- user
      |- register.html
      |- register.txt

Depending on the users language different layouts and templates will be rendered.

At the moment i extend / overwrite your function

ExpressHandlebars.prototype._resolveLayoutPathOld = express3Handlebars.ExpressHandlebars.prototype._resolveLayoutPath;
ExpressHandlebars.prototype._resolveLayoutPath = function(layoutPath) {
  layoutPath = this._resolveLayoutPathOld(layoutPath);
  return layoutPath.replace(/%language%/, this.language);
};

var templateHtml = express3Handlebars.create({
  helpers: templateHelpers,
  defaultLayout: "main_mail.html",
  layoutsDir: "views/%language%/layouts/"
});

var templateText = express3Handlebars.create({
  helpers: templateHelpers,
  defaultLayout: "main_mail.txt",
  layoutsDir: "views/%language%/layouts/"
});

Before render i set de ExpressHandlebars.language variable.

Call render with language Param for the template

app.render(language + template + ".html", fields, ....

Nice will be a param in the fields-param, when the template got rendered and this param will be uses for layouts too.

@ericf
Copy link
Owner

ericf commented Mar 22, 2013

This sounds like an important feature to add, and if I can make a simple change to easily support what you're doing, I'm all for that!

To clarify, are you asking that I should pass the entire app/response locals object (fields in your example) to the _resolveLayoutPath() method?

Which would allow the following:

var app = require('express')(), 
    hbs = require('express3-handlebars').create({ /* ... */ });

app.engine('handlebars', hbs.engine);

hbs._resolveLayoutPath = function (locals) {
    var layoutPath = this.constructor.prototype._resolveLayoutPath.apply(this, arguments);
    return layoutPath.replace(/%language%/, locals.language);
};

// ...

app.render('foo.handlebars', {language: 'de'});

@rottmann
Copy link
Author

Yes that will be a great solution. 👍

@ericf ericf closed this as completed in 05e8d88 Mar 22, 2013
@ericf
Copy link
Owner

ericf commented Mar 22, 2013

@rottmann Okay, this change has been published to npm. Let me know if you run into anything else so we can keep thinking about adding first class multilingual support if the need arises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants