i18next integration for hapijs
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
example
src
typings
.editorconfig
.gitignore
LICENSE
README.md
gulpfile.js
index.js
package.json
tsd.json

README.md

hapi-i18next

i18next integration for hapijs

Installation

This plugin is available as an npm module. Simply run: npm install --save hapi-i18next.

Getting Started

This plugin sets up and registers two methods in your hapi server: server.methods.i18n.getInstance() and server.methods.i18n.translateWithCache. Using hapi server methods allows us to set up caching schemes for the translate method. Examples of how to integrate these server methods into your application:

// Registering plugin exposes two server methods that you can use immediately in your handlers
// server.methods.i18n.getInstance
// server.methods.i18n.translateWithCache
server.register({
    register: require('hapi-i18next'),
    options: {
        // Standard options passed into i18next initialization http://i18next.com/node/pages/doc_init.html
        i18nextOptions: {
            resGetPath: path.join(__dirname, 'locales/__lng__/__ns__.json'),
            ns: 'translations',
            detectLngFromQueryString: 'lang',
            detectLngFromPath: 0,
            cookieName: 'test',
            supportedLngs: ['en', 'de']
        }
    },
    cookieOptions: {
      // http://hapijs.com/tutorials/cookies
    }
}, function (err) {});

// Using it in views requires a few additional steps
server.views({
    // pass our server methods to global view context, so that we may use it in helpers
    context: {
        i18n: {
            // This step is left up to the developer, but the most declarative way to use this plugin is to add it's methods
            // to the global context of your views. That way you can declare a template helper such as the {{t 'hello-world'}}
            // in this folder to use as common interface for i18n messages.
            translateWithCache: server.methods.i18n.translateWithCache,
            getInstance: server.methods.i18n.getInstance
        }
    }
});

In your helper, t.js:

function i18nHelper(key, options) {
    var translateWithCache = this.i18n.translateWithCache,
        params = {},
        namespace = '',
        instance = this.i18n.getInstance();

    Object.keys(options.hash).forEach(function (key) {
        if (key === 'ns') {
            namespace = options.hash[key] + ':';
        }
        else if (options.hasOwnProperty(key)) {
            params[key] = String(options.hash[key]);
        }
    });
    return translateWithCache(namespace + key, instance.lng(), params);
}
module.exports = i18nHelper;