Node.js internationalization and form validation

In any application, no matter the languages or the environment you are using, it is always necessary to ensure that all data is validated before we try any operation. It's tedious, boring and maybe you prefer to focus on more interesting things but you may fear not only hackers, but also the users who from time to time become true hackers without been really aware of it: just applying unexpected input. For instance a form field could ask for the zip code and a regular user could answer (sincerely) 'I don't know'. And he is being honest but at the same time he is creating an exception and blowing out your super-duper node server.

Form validation

In addition to that and in order to be nice with a wider range of users you have to offer internationalization support. Once again, a boring but a mandatory task to take into account when you are dealing with the views. In this post I'll show one approach to solve these issues node.js trying to keep the code simple. Let's start with i18n.

Internationalization in Node.js

For Node.js and in general for any javascript based software we have then i18next package. It comes with all you need to write multilingual applications, many available options for the language file format and many niceties as well. i18next can be used in both server and client side javascript. In this case I'll show just the translation files format, how to init i18next and how to apply in jade templates. In the event that you're using Angular (or maybe something not so mainstream :P) framework in the frontend, it is possible to reuse the work done in server side.

Language files

In the root of your project, just place a directory called locales and inside it one directory for each language you give support to (locales/en, locales/es), including locales/dev/ which is the lang by default. And inside each directory, place a json file called translation.json. So we are using json format, but there are other formats available, and many other options including namespaces support.

This is how the translation.json file looks like. Note that you can organize translations in groups

Applying i18next

In our main program, app.js, we will have to require, init and apply i18next to our express application to make it available for jade templates. This is app.js now, some init alternatives are commented. The loading of the translation files is done asynchronously.

This is how we apply i18n in a jade template using function t(key), where key must have an entry in the translation files.

Simple form validation

As simple as using validation facilities from the mongoose driver. There are another options but in this case I wanted to make use of something that I already have instead of trying other packages. Check these modules if you need something else or if you have forms that have nothing to do with an underlying database. Anyway, if we are using mongoose, we'll have to create validations.

MongoDB is a schemaless document-oriented database and it can handle anything: each row in a collection could be completely different. However, Mongoose driver is much more than a simple way to access a database: it forces you to define schemas to map each collection in database, where at least you have to detail fields and data types. In a way, that is a first defense line. In those schema definitions (which deserve another long post devoted to mongoose exclusively) is where we can apply validations (built-in and custom), limits and even define custom error messages! Here I'm improving the schema for the guestbook collection.

Internationalization in mongoose validation messages

I'm applying i18n in the mongoose schema in a ... cryptic way. There is a problem if we try to use the i18next.t(key) function to apply messages in schemas. In the main app.js we are loading express routes. Routes are requiring schemas and as i18next are not loaded synchronously (and init options for that are not working as expected) so those t(key) calls in mongoose models will fail. We could refactor and reshape the main app.js, we could maybe use packages to force synchronous behaviour, but It is much more easier to use the key from translation files, which are simple strings.

This is the guestbook.jade template, now with some error handling conditionals to show error messages. Mongoose stores all validation information in an err object which we simply pass to this jade template to be displayed.

And finally the router where we get validation data from mongoose and we move to the template to be rendered.

Once again I tried to keep main app.js simple. There may be many other (and better) recipes because Node.js is like a bazaar with thousands of very active contributors. Greets to any.

by Pello Altadill 23 Hours ago - 28 hits

Keeping Node.js app.js clean + custom middleware for express

This post has two purposes: to continue the previous one showing ways to improve the organization of routes and secondly to see how to add your own middleware to an express application. The previous version have a problem when it comes to the routes. We require all of them with a single requ...

by Pello Altadill 6 Days ago - 118 hits

More »

Node.js + express routes + mongoose, simple and separated

There are plenty of examples out there explaining how to develop a webserver in node.js using the express framework. There are also great books like Professional Node.js and Smashing Node.js introducing details about express among many other topics. But there is also, imho, a big problem: all of them show different ways to solve the same problem. In addition to that there were changes in the framework (Express has recently changed to version 4) and some documentation and samples could be deprecated, so if y...

by Pello Altadill 07/23/2014 23:45:40 - 213 hits

More »

Setting different config files in Node.js

External config files When it comes to configuration options it is always desirable to avoid hardcoding config values in our code. Even more, if we are using different config values in development and production environments it is necessary an easy way to switch from one to another. This also becomes useful when your code is public but you want to hide some config files. In Node.j...

by Pello Altadill 07/22/2014 00:52:26 - 192 hits

More »

Enabling basic authentication for MongoDB databases

MongoDB is extremely easy to install and run. If you are not able to make it work maybe you could feel more confortable using Access or even excel. Well, sorry for that. This short article is but a quick and dirty guide to enable basic authentication in mongodb, and how to create one database with its own user (not a superadmin) with just read/write permissions. If you run mongo...

by Pello Altadill 07/20/2014 00:37:15 - 224 hits

More »

Python language basics

Python in 21 minutes (or so) For a developer is not enough to know one programming language, it is often said that is good to know many of them. After all the language is but a mere tool to build programs. Python has been around for many years and one of my pending task was to take a look at it. Some friends (@Eugenia4v,@Claw_Shadow,@D00m3dr4v3n) are always tellin...

by Pello Altadill 07/12/2014 23:51:05 - 332 hits

More »

A night out with Jasmine

A night out with Jasmine Unit testing is much more than a tool for ensuring that a program behaves as expected. Nowadays it's the key for new development techniques such as TDD and BDD in which you begin writing tests before the required code. It's all about requirements and behaviour; developers must focus on understanding the requirements instead of thinking about the way they implement the code. In the event that you've forgotten the fundamental benefits of unit testing here you have a quick...

by Pello Altadill 04/23/2014 00:37:18 - 1696 hits

More »


This a classic pong-like game that takes some ideas from Arkanoid. Basically two paddles try to hit the ball until one of them fails but, from time to time the rules can change if special bricks are touched. Red: invert ball direction White: triple ball Yellow: smaller paddles Blue: bigger paddles Green: speed up ball Orange: change...

by Pello Altadill 04/07/2014 07:35:33 - 1676 hits

More »

Getting started with Node

Few days ago, a very dear friend of mine came up to my place with a present. It was a nodejs book. This is a new or maybe it would be more accurate to say an emergent technology which brings back an old idea that I used to hear of long time ago: a server side javascript. That was something that was supposed to exist somewhere, as it was mentioned in the prefaces of many javascript related papers: ...

by Pello Altadill 03/17/2014 23:48:44 - 1938 hits

More »