Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

access language in request express

// place this middleware before declaring any routes
app.use((req, res, next) => {
    // This reads the accept-language header
    // and returns the language if found or false if not
    const lang = req.acceptsLanguages('bg', 'en')
    
    if (lang) { // if found, attach it as property to the request
        req.lang = lang
    } else { // else set the default language
        req.lang = 'en'
    }

    next()
})
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #access #language #request #express
ADD COMMENT
Topic
Name
7+6 =