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

mecore-hapi-i18n-multi-instance

v3.0.1

Published

Translation module for hapi based on mashpie's i18n module

Downloads

6

Readme

hapi-i18n

Translation module for hapi based on mashpie's i18n module.

The latest version is for Hapi 17+. For Hapi versions < 17 use version 1.0.5

Installation

npm install hapi-i18n

Usage

For details see the examples in the mocha tests.

The i18n module is attached to the request object configured with the requested locale. This ensures that the correct locale is set for the request when processing multiple requests at the same time.

JavaScript

function (request, h){
  return {
    message: request.i18n.__('My localized string')
  };
});

Pug Template

doctype html
html(lang=languageCode)
  body
    p!= __("My localized string")
    p!= __("hello", {name:"Manu"})

Nunjucks Template

<p>{{ __("My localized string") }}</p>
<p>{{ __("hello", {name:"Manu"}) }}</p>
<p>{{ __("hello", name="Manu2") }}</p>

Handlebars Template

<p>{{#i18n "My localized string"}}{{/i18n}}</p>

For Handlebars you need to specify a helper:

Handlebars.registerHelper('i18n', function(context) {
  return this.__(context);
});
server.route({
  ...
  options: {
    handler: function (request, h) {
      return h.view('A localized webpage', {
        ...
        languageCode: request.params.languageCode
      })
    }
  }
});

Register Plugin

There are three possibilities to pass and read the language code.

Path parameter

The first option is passing the language code with a path parameter. The basic configuration to define the supported locales and the directory to load the translation files from is as follows:

await server.register({
  plugin: require('hapi-i18n'),
  options: {
    locales: ['de', 'en', 'fr'],
    directory: __dirname + '/locales'
  });
}

The configuration options are passed directly to mashpie's i18n module. To get the full list of available options see mashpie/i18n-node. The default locale is the first locale found in the list, in this example "de".

The requested language is specified by a path parameter languageCode in your resource urls:

server.route({
  method: 'GET',
  path: '/{languageCode}/localized/resource',
  options: {
    handler: function (request, h) {
      return (
        {
          message: request.i18n.__('My localized string')
        }
      );
    }
  }
});

Example request:

http://localhost/fr/localized/resource.

The language code is evaluated automatically. If a language code is found for the requested path parameter then the according locale is set. If the language code does not match any of the configured language codes, the plugin returns 404 (NotFound).

Language code from the request header

The second option is reading the language code from the request header:

await server.register({
  plugin: require('hapi-i18n'),
  options: {
    locales: ['de', 'en', 'fr'],
    directory: __dirname + '/locales',
    languageHeaderField: 'language'
  });
}

Query parameter

A third option is passing the language code with a query parameter (plugin option queryParameter). Example:

await server.register({
  plugin: require('hapi-i18n'),
  options: {
    locales: ['de', 'en', 'fr'],
    directory: __dirname + '/locales',
    queryParameter: 'lang'
  });
}

The requested locale can be passed with the lang query parameter. Example request:

http://localhost/my/localized/resource?lang=fr.

If the language code does not match any of the configured language codes, the plugin returns 404 (NotFound).

Define default locale

If no locale is defined, the default locale is selected. By default, the default locale is the first element in the locales option. However, you can specify this with the defaultLocale parameter :

await server.register({
  plugin: require('hapi-i18n'),
  options: {
    locales: ['de', 'en', 'fr'],
    directory: __dirname + '/locales',
    defaultLocale: 'en'
  });
}