2012-09-02 81 views

回答

1

如果您碰巧使用着名的express服務器,則存在執行該工作的中間件。 request-language解析Accept-Language標題並返回最適合您的語言:

var requestLanguage = require('express-request-language'); 
var cookieParser = require('cookie-parser'); 
var express = require('express'); 
var app = express(); 

app.use(cookieParser()); 
app.use(requestLanguage({ 
    languages: ['en-US', 'zh-CN'], 
    cookie: { 
    name: 'language', 
    options: { maxAge: 24*3600*1000 }, 
    url: '/languages/{language}' 
    } 
})); 

app.get('/', function(req, res, next) { 
    console.log(req.language); // 'en-US' 
});