Working Pig Working Pig - 12 days ago 7
Node.js Question

How should I create multilanguages in nodejs without cookie and extern modules

I'm currently trying to transfer all of my projects to node.js to improve my Javascript skills, I'm using Node.js with Express module. One of my client sites is a translate company, they want me to add 2 different languages to their site, English and Dutch, my first idea was basically write and reade cookies, but they don't want to use any cookies(because of cookie popout), insteed of it they want to determine language in the url like this: site.com/en/routing and site.com/nl/routing Its easy to me to set two different routings like that:

app.use('/en', require('./routes/en'));
app.use('/nl', require('./routes/nl'));


I have different mongodb tables:
content_en and content_nl

In one all is in English and another in Dutch.

I don't want to copy all functions from /en with table content_en and in /nl rename it to content_nl, I'm looking for better solution to it,
but I don't know any. Can you help me? I have of course, more than 1 page, I have different pages, and I need to build cms, to add data and edit data.
I want to use database for it.

rsp rsp
Answer

Instead of using:

app.use('/en', require('./routes/en'));
app.use('/nl', require('./routes/nl'));

and then having for example the same route for both languages like:

.get('/info', ...)

You can make one route that is:

app.get('/:lang/info', ...)

and access req.params.lang inside.

So e.g. the table name would be:

var table = 'content_' + req.params.lang;

which would be equal to "content_en" if the route was accessed as /en/info and "content_nl" if it was accessed as /nl/info.