GithubHelp home page GithubHelp logo

rluba / hapi-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from funktionswerk/hapi-i18n

0.0 2.0 0.0 44 KB

Translation module for hapi based on mashpie's i18n module

License: MIT License

JavaScript 99.15% HTML 0.85%

hapi-i18n's Introduction

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')
  };
});

Jade 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'
  });
}

hapi-i18n's People

Contributors

codeva avatar kaywolter avatar odorisioe avatar rapoell avatar rluba avatar throrin19 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.