0.4.0 • Published 4 years ago

express-langs v0.4.0

Weekly downloads
2
License
BSD-2-Clause
Repository
github
Last release
4 years ago

express-langs

npm npm version

build travis codecov

parse langs from request for easier use with express

screen shot demo

resolve langagues accept in header request get array langagues with request.langs

Installation

npm install express-langs --save

yarn add express-langs

server.js

const
    exp = require('express')
    ,app  =exp()
    ,server = require('http').Server( app )
    ,langs = require('express-langs')
;

app
    // ... , you other middleware
    .use( langs )
    // you other middleware ... ,
;


app
    .get('/' , (req,res) => {

        /**
         * array<object>
         *  {
         *      lang: string
         *      ,val: float
         *  }
         */
        console.log( req.langs.items );

        res.send( 'hello world !' ) ;

    } )
;

server.listen( 80 , () => console.log('server run ...') ) ;

you can use methods test exists langs

const
    exp = require('express')
    ,app  =exp()
    ,server = require('http').Server( app )
    ,langs = require('express-langs')
;

app
    // ... , you other middleware
    .use( langs )
    // you other middleware ... ,
;


app
    .get('/' , (req,res) => {

        const langs = req.langs ;

        if( langs.exists( 'fr' ) ) {


            res.send( 'salut le monde !' ) ;

        } else {

            res.send('hello world') ;
        }

    } )
;

server.listen( 80 , () => console.log('server run ...') ) ;

exists methods make a relative test with langs found e.g :

langs.exists('en') ; // match with 'en-US'
langs.exists('en') ; // match with 'en'
langs.exists('en') ; // not match with 'fr'

or with an pattern e.g :

langs.exists( /en/i ) ; // match with 'en-US'
langs.exists( /en/i ) ; // match with 'en'

langs.exists( /en|fr/i ) ; // match with 'fr'

you can make a strict test exists e.g :

langs.existsStrict('en') ; // not match with 'en-US'
langs.existsStrict('en') ; // match with 'en'
langs.existsStrict('en') ; // not match with 'fr'

getWith method get array langs match with you support pattern

const supportLangs = langs.getWith( /en|fr|es/i ) ; // array[ langs ]

// you can give an string for build an pattern e.g :
const pattern = "en|fr|es" ;
const supportLangs = langs.getWith( pattern ) ; // array[ langs ]

develop by Samuel Gaborieau with <3 and NodeJS for Open Source and enjoy !

0.4.0

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.0

4 years ago

0.2.63

4 years ago

0.2.62

4 years ago

0.2.61

4 years ago

0.2.6

4 years ago

0.2.51

4 years ago

0.2.5

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.82

4 years ago

0.1.81

4 years ago

0.1.8

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago