0

I'm very new to Node.js and Express.js development and I'm looking for a simple and easy solution for creating multi language routes using i18n module.

I would like to achieve that urls in my app look like this:

www.myapp.com/en/about

www.myapp.com/de/about

www.myapp.com/fr/about

Than I would like to get the language as a string from url and sand it to the view as a variable. Like so:

app.get("/:lang/about",function(req,res){
    res.render("about",{language: lang}); 
});

The default language would be English - en.
Please help.

denisr
  • 123
  • 5

1 Answers1

0

You can use req.params to get the URL parameters. You can make lang an optional parameter, then set it to "en" if it is undefined. This will default it to english if the user load /about with no language specified.

app.get("/:lang*?/about",function(req,res){
    var lang = req.params.lang;
    if (lang === undefined) {
        lang = "en"
    }

    res.render("about",{language: lang}); 
});
Zeke Snider
  • 486
  • 5
  • 9