Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Last active August 29, 2015 14:24
Show Gist options
  • Save oberhamsi/36030f277dd30cf70dc3 to your computer and use it in GitHub Desktop.
Save oberhamsi/36030f277dd30cf70dc3 to your computer and use it in GitHub Desktop.
reinhardt i18n strawman
var {Application} = require('stick');
var {Reinhardt} = require('reinhardt');
var ResBundle = require("ilib/lib/ResBundle");
// Resources is the interface that reinhardt requires
var Resources = function(locale) {
this.rb = new ResBundle({
locale: locale,
type: "html",
loadParams: {
base: module.resolve("path/to/resources")
}
});
};
Resources.prototype.translate = function(source, key) {
return this.rb.getStringJS(source, key);
};
// from config file or command-line?
var defaultLocale = howeverYouGetThis();
var resourceCache = module.singleton('ResCache', function() { return {} });
var env = new Reinhardt({
i18n: {
resBundleFactory: function(request) {
var locale = request.session.locale || defaultLocale;
if (!resourceCache[locale]) {
resourceCache[locale] = new Resources(locale);
}
// resources are immutable, so we don't have to worry about sharing them amongst sessions and threads
return resourceCache[locale];
},
locale: defaultLocale
}
});
var app = exports.app = new Application();
app.configure('session', 'params', 'locale', 'route');
app.get("/", function(request) {
// good thing is users do not need any changes to most of their code like this!
// The foo.html template will just come out in the right locale
return env.renderPage("foo.html", context);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment