[英]Pass language parameters into jade links
I have a multi-language website which uses language identifiers in the hyperlinks eg, /en/contact for the English contact page and /de/contact for a German contact page. 我有一个多语言网站,该网站在超链接中使用语言标识符,例如,英语联系人页面的/ en / contact和德语联系人页面的/ de / contact。
If I cross link on my page, for example from the index page to the contact page, how can I "pass" the language part in my hyperlink? 如果我在页面上交叉链接(例如,从索引页面到联系页面),如何在超链接中“传递”语言部分? Depending on if I am on /en/index or on /de/index, the hyperlink needs to either point to /en/contact or /de/contact. 根据我是在/ en / index上还是在/ de / index上,超链接需要指向/ en / contact或/ de / contact。
I am using Express + Jade. 我正在使用Express + Jade。 There is one jade template for both languages which needs to set the link dynamically depending on if the user is on an /en or /de page. 两种语言都有一个jade模板,需要根据用户在/ en或/ de页面上来动态设置链接。
Thanks a lot in advance! 在此先多谢!
This is the simplest example I can think of. 这是我能想到的最简单的例子。 Use a middleware function to inspect the request and pull out the language, then set that as a variable that will be available to your template. 使用中间件功能检查请求并提取语言,然后将其设置为模板可用的变量。 You may be able to use more of a global middleware but this example shows it on a single route: 您也许可以使用更多的全局中间件,但是此示例在一条路径上显示了它:
app.use('/:userLang/contact', i8n, users);
function i8n(req, res, next) {
var supportedLanguages = ['en', 'de'];
var defaultLang = 'en';
var pathLang = req.params.userLang;
// default to en if a user trys a language that isn't supported
// another(possibly better) option would be to 404 or 301 to the en version
res.locals.userLang = (supportedLanguages.indexOf(pathLang) > -1)
? req.params.userLang : defaultLang;
next();
}
Once the above code is in place links in your template could look like this: 上面的代码到位后,模板中的链接可能如下所示:
a(href="/#{userLang}/contact) Contact Page
在联系页面上,您可以将用户发送到英语索引页面,前提是您在英语联系页面上
a(href="/en/index")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.