Open drewda opened 10 years ago
I'm sure it will, as i18n is generally "framework agnostic". The examples provided show only a few of possible integrations.
In general i18n could get attached to any object, like so:
var anyObject = {
__: i18n.__
};
i18n.configure({
locales: ['en', 'de'],
register: anyObject
});
anyObject.setLocale('en');
anyObject.__('Hallo'); // --> Hello
or, coul be used as middleware, by providing the common i18n.init(req, res)
interface.
I am not familiar with hapi, but will take a look...
Thanks for your quick response and for the example code! On Aug 1, 2014 2:13 AM, "Marcus Spiegel" notifications@github.com wrote:
I'm sure it will, as i18n is generally "framework agnostic". The examples provided show only a few of possible integrations.
In general i18n could get attached to any object, like so:
var anyObject = { : i18n.}; i18n.configure({ locales: ['en', 'de'], register: anyObject}); anyObject.setLocale('en');anyObject.__('Hallo'); // --> Hello
or, coul be used as middleware, by providing the common i18n.init(req, res) interface.
I am not familiar with hapi, but will take a look...
— Reply to this email directly or view it on GitHub https://github.com/mashpie/i18n-node/issues/121#issuecomment-50864557.
After fiddling around for some time i came up with this solution. So if anyone is interested here is the code. Basically it attaches the i18n interface to every view. Feedback welcome :)
var i18n = require('i18n'),
extend = require('util')._extend;
exports.register = function(plugin, options, next) {
// we need to check if a register object was defined,
// since we have to attach it later to the request.response.source.context
options.register = options.register || {};
// Load settings and bind i18n interface to options.register
i18n.configure(options);
// Bind interface to global context
// See: http://hapijs.com/tutorials/views#global-context
plugin.ext('onPreResponse', function(request, reply) {
var source = request.response.source;
// Attach interface only if we got a view
if (request.response.variety === 'view') {
// Initialize i18n on request and response
// Not quite sure if response is needed at all? Works fine without too
i18n.init(request, request.response);
// Use 'accept-language' header to guess language settings
i18n.setLocale(i18n.getLocale(request));
// Attach the interface to the view
source.context = extend(source.context || {}, options.register);
}
reply();
});
next();
};
exports.register.attributes = {
name: 'i18n'
};
thank you @davidspiess
note to myself: express hapi usage in examples
Can i18n-node support the Hapi framework? If so, how about including an example?