Giter Club home page Giter Club logo

accept's Introduction

gengojs-accept

Express, Koa, Hapi locale parser that powers gengo.js.

Gitter

Build Status Dependency Status License Status Downloads Version

This module parses the accept-language header from Express, Koa, or Hapi and returns the appropriate locale.

Documentation

See the beautifully generated documenation at GitHub.

Usage

$ npm i --save gengojs-accept

Express

import express from 'express';
import accept from 'gengojs-accept/express';

let app = express();

app.use(accept());

app.use('/', function(req, res, next){
	console.log(req.getLocale());
});

// ...

Hapi

import Hapi from 'hapi';
import accept from 'gengojs-accept/hapi';

let server = new Hapi.Server();
server.connection({
	port:3000
})
 
server.register(accept(), function(error){
 	if(error) console.log(error);
});

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        console.log(request.accept.getLocale());
        reply();
    }
});

// ...

Koa

Note: I have not tested Koa's v1.0.0 API but in theory it should work. Please report any bugs if encountered or create a pull request to add tests.

import Koa from 'koa';
import accept from 'gengojs-accept/koa';

let app = new Koa();

app.use(accept());

app.use('/', function (self, next){
	console.log(self.accept.getLocale());
    // or
    console.log(self.request.accept.getLocale());
    // or
    console.log(self.response.accept.getLocale());
});

// ...

Standalone

import accept from 'gengojs-accept';

// pass the request object from express or hapi 
// or the 'ctx'/'self' context from koa;
let currentLocale = accept(req || ctx, options).getLocale();

Options

{
    "check": true,
    "default": "en-US",
    "supported": [
        "en-US"
    ],
    "keys": {
        "cookie": "locale",
        "query": "locale"
    },
    "detect": {
        "header": true,
        "cookie": false,
        "query": false,
        "url": false,
        "domain": false,
        "subdomain": false
    }
}  

accept's People

Contributors

gitter-badger avatar iwatakeshi avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

gitter-badger

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.