npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-i18next

v2.2.0

Published

hapijs plugin for i18next

Downloads

200

Readme

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;