mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.08k stars 419 forks source link

Support for Hapi #121

Open drewda opened 10 years ago

drewda commented 10 years ago

Can i18n-node support the Hapi framework? If so, how about including an example?

mashpie commented 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...

drewda commented 10 years ago

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.

davidspiess commented 9 years ago

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'
};
mashpie commented 9 years ago

thank you @davidspiess

note to myself: express hapi usage in examples