简体   繁体   English

nodejs express 中的翻译 url 路由

[英]Routing for translation url in nodejs express

I would like to know how to do routing for translation url in nodejs express我想知道如何在 nodejs express 中为翻译 url 做路由

I have following routes in app.js , I would like to know how to do in a better way, lets say if have more than 5 languages , url will vary according to language but go to same route.我在app.js有以下路线,我想知道如何以更好的方式做,假设有超过 5 种语言,url 会因语言而异,但会转到相同的路线。 How to do in express nodejs.如何在 express nodejs 中做。

app.js
app.use(/^\/(en|de)/, langRouter);
app.use(/^\/(en|de)\/((all-services-from|hui-kuan-cong)-(.+)-(to|zhi)-(.+))/, serviceRouter);
app.use('/:lang/route-services-services/:pr', aboutRouter);
app.use('/:lang/ain-jian-wen-ti/:pr', aboutRouter);


frontend urls,
will pass to langRouter
/en 
/de
will pass to serviceRouter
/en/all-services-from-sin-to-mal
/de/hui-kuan-cong-sin-zhi-mal
will pass to aboutRouter
/en/route-services-services/fund
/de/ain-jian-wen-ti/fund

app.use(/:locale*, checkLangRouter);

app.use(/:locale/, langRouter);

app.use(/:locale/:slug/, serviceRouter)

app.use('/:locale/:slug/:pr', aboutRouter);

The first is the middleware to check if the locale is available.. 第一个是用于检查语言环境是否可用的中间件。

In each router, check the slug depending of the locale. 在每个路由器中,根据地区检查该段标签。 If it's not corresponding, just call the next() method... 如果不对应,只需调用next()方法即可。

//aboutRouter.js

module.exports = (req, res, next) => {
    const locale = req.params.locale;
    const slug = req.params.slug;

    const myMapping = {
         en: 'about',
         fr: 'a-propos',
         it: 'attorno'
    };

    if (myMapping[locale] !== slug) {
         // It's not the about route
         return next();
    }
};

In this case, a conceil will be to export the mapping in another file to make it readable... 在这种情况下,一个想法就是将映射导出到另一个文件中以使其可读。

In translation in nodeJs Express use i18n ... very simple and easy在 nodeJs Express 中的翻译中使用i18n ... 非常简单易行

https://lokalise.com/blog/node-js-i18n-express-js-localization/ https://lokalise.com/blog/node-js-i18n-express-js-localization/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM