我有一个带有Cloudflare的Heroku应用程序,我尝试根据客户端的本地化显示不同的版本。它在开发中工作得很好,但在生产中并非如此(总是显示/en,而不是/fr)。我使用express-ip npm包。
代码:
const express = require('express');
const router = express.Router();
const expressip = require("express-ip");
router.use(expressip().getIpInfoMiddleware);
router.get("/", function ipFrance(req, res) {
const ipInfo = req.ipInfo;
const ipInfoRegion = req.ipInfo.region;
const ipInfoCountry = req.ipInfo.country;
//var message = `Hey, you are browsing from ${ipInfoRegion}, ${ipInfoCountry}`;
if(ipInfoCountry == "FR" || ipInfoRegion == "Wallonia") {
res.redirect("/fr");
} else {
res.redirect("/en");
}
});
module.exports = router; 发布于 2019-12-20 23:53:58
不提供基于IP地址的转换。There's an HTTP header for that,以及使用该标头的express API方法req.acceptsLanguages():
router.get("/", function (req, res) {
if (req.acceptsLanguages("fr")) {
res.redirect("/fr");
} else {
res.redirect("/en");
}
});一些以法语为母语的人可能喜欢用英语浏览,而世界上的其他地方可能更喜欢用法语浏览。让他们做决定,而不是为他们做决定。
https://stackoverflow.com/questions/59427790
复制相似问题