Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-language routes in express.js?

I'm wondering if there is a best practise example on how to implement multi-lanuage routes in express.js. i want to use the accept-language header to get the browser language and then redirect automatically to the corresponding language route like

www.foo.bar/de/startseite OR www.foo.bar/en/home

Any advice on this?

like image 935
trnc Avatar asked Aug 29 '12 21:08

trnc


People also ask

Is Express js used for routing?

js server. The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.

Is Express JS full stack?

Two common full-stack JavaScript stacks are: MEAN stack - MongoDB (database), Express (server), Angular (front-end framework), and Node (runtime environment)

Is Express JS synchronous or asynchronous?

While Express will automagically catch synchronous errors and pass them to our custom error handler, asynchronous errors are a whole different beast. If a promise is rejected without being caught in an express route handler, the unhandled rejection will prevent the client from receiving any response!

Is Express JS good for big projects?

Middleware frameworks, like Express. js, are suitable for small and medium projects. If you are going to develop a large project that will be supported by a large team of developers, Express. js is not the best choice.


2 Answers

i have done the following: install i18n-node modul and register in the express js. here is code.

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , i18n = require("i18n");

  var app = express();

i18n.configure({
    // setup some locales - other locales default to en silently
    locales:['de', 'en'],
    // disable locale file updates
    updateFiles: false
});

app.configure(function(){
  ...
  app.use(i18n.init);
  ...
});
// register helpers for use in templates
app.locals({
  __i: i18n.__,
  __n: i18n.__n
});

after this set the following to get all request

// invoked before each action
app.all('*', function(req, res, next) {
    // set locale
    var rxLocal = /^\/(de|en)/i;
    if(rxLocal.test(req.url)){
        var arr = rxLocal.exec(req.url);
        var local=arr[1];
        i18n.setLocale(local);
    } else {
        i18n.setLocale('de');
    }
    // add extra logic
    next();
});

app.get(/\/(de|en)\/login/i, routes.login);

maybe this help.

like image 120
Mirodil Avatar answered Oct 19 '22 02:10

Mirodil


I'd just serve up the content in the detected language directly.

For example, example.com/home serves up the home page in the best available Accept-Language (possibly overridden by cookie if you provide a language selection option on the site itself).

You'd want to make sure that your response's Vary: header includes Accept-Language.

IMO, including language codes in the URI is an ugly hack. The RFC's intent is that a single resource (your home page) is universally represented by a single URI. The entity returned for a URI can vary based on other information, such as language preferences.

Consider what happens when a German-speaking user copies a URL and sends it to an English-speaking user. That recipient would prefer to see your site in English, but because he has received a link that points to example.com/de/startseite, he goes straight to the German version.

Obviously, this isn't ideal for full internationalization of what the user sees in the address bar (since home is English), but it's more in line with the RFCs' intent, and I'd argue it works better for users, especially as links get spread around email/social/whatever.

like image 33
josh3736 Avatar answered Oct 19 '22 03:10

josh3736