camille camille - 3 months ago 13
Node.js Question

Multilingual NodeJs API

i'm developing a REST API in nodejs with swagger and mongoDB.

I am looking for a module or a good practice to help me with multi-language support.
Every objects from collections should have their own translation and i am not sure about the best way to do this.

I had a look to several modules but everything i found is for static data.

The translation is up to the user and can be posted at anytime, in any languages. I need to make sure the user can retrieves his data in every language he posted.

here an example of what i was thinking to do, but i'm not sure this is a recommended practice.

Collection : apartments

Data in english:

var apartment = {
"id": "1x213"
"name": "Flat 1",
"price": 100000,
"rooms": 2,
"city": "paris",
"country": "france",
"zipcode": 75016,
"address": "11 avenue du general de Gaulles",
"description": "A wonderful apartment at 2 minutes from the eiffel tower"
}


Data in french:

var apartment = {
"id": "1x214"
"name": "Appartement 1",
"price": 100000,
"rooms": 2,
"city": "paris",
"country": "france",
"zipcode": 75016,
"address": "11 avenue du general de Gaulles",
"description": "Un magnifique appartement, à 2 minutes de la tour eiffel"
}


Collection : multilanguage

var multilanguage = {
"id": "1y679"
"language": [{'en': '1x213' },
{ 'fr': '1x214'}]
}


I read about some other practice here but i'm not sure it's alright for big purpose. It seems a bit messy to me.

thank you

Answer

I finally chose the following method. I think it's the easiest and more scalable way to do it. I drew inspiration from ruby on rails good practice.

data in english :

var apartment = {
    "id": "1x213",
    "group_id": "c3por2d2",
    "language": "en",
    "name": "Flat 1",
    "price": 100000,
    "rooms": 2,
    "city": "paris",
    "country": "france",
    "zipcode": 75016,
    "address": "11 avenue du general de Gaulles",
    "description": "A wonderful apartment at 2 minutes from the eiffel tower"
    }

data in french :

var apartment = {
    "id": "1x214",
    "group_id": "c3por2d2",
    "language": "fr",
    "name": "apartment 1",
    "price": 100000,
    "rooms": 2,
    "city": "paris",
    "country": "france",
    "zipcode": 75016,
    "address": "11 avenue du general de Gaulles",
    "description": "Un magnifique appartement, à 2 minutes de la tour eiffel"
    }

I've got now a common group_id that i can query to get the data in every language the user posted, i also can query data in a specific language.