5

Say I want to build a website that support multiple language and use MongoDB datastore. I wonder what is a good approach to have multiple versions of the body and title of an article, one for each language.

Should I keep all versions in the same article document? Like a collection of key-value pairs of language code and text in the body field of the article. This way I'd get no more than 1 request for viewing articles, but I'd also get more data than I might need so the response is larger.

Is there a better solution? How and why?

2

I use the following pattern for text that should be indexed in all the languages:

{
"id":"sdsd"
"title":{"languages":{"en":0,"fr":1},"texts":["this is the title in inglish","Celui ci c'est le titre en francais"]}
}

object = coleccion.find("id='xxxxx'");

// now if want the text in English

print(object.title.texts[object.title.languages["en"]])

// the use of objecttitle.languages index array it to improve performance in client accessing a determined text

// that allows us to add indexes to your translated texts on mongo, as

ensureIndex({title.texts})

We can also wrap the code to obtain text in an specific language in a Class.

  • Why do you have a language object inside the title property? Why not use the language code as indexer? {'id': '123', 'title': {'en': 'english', 'sv': 'swedish' } } and then to access the title object.title['en'], alternatively add a getTitle() method that get's the title according to the user's locale. My main concern is whether I should keep meta and content in separate documents or not. What is more efficient? A single large document with all versions of an article, or one document for metadata and and separate documents with content for every language. – Gabriel Smoljár May 31 '13 at 19:23
  • Hello, It really depends in how are you planning to access your data and from where comes your data, in my case, I have "titles" that are translated on English and another "titles" that are translated on German and not neccessary have a version on English. And I want to index (Using Mongo EnsureIndex) on all the texts fields of my collaction, It is why I need to have the texts in an array, If I had the texts as you proposed ('title':{'en','english,'sv':'swedish'}) then I would be unable to index all the texts without knowing which languages to index. – Fer Jun 3 '13 at 9:40
  • I usually have metadata in the same document as this data is small, if there is a lot of metadata, then I could think in separates documents, or only asking parts of the document when querying the data. I thing from the mongo point of view, to optimize performance, all depends on your indexes and the grannularity of the quary (I mean you can ask only parts of a document) – Fer Jun 3 '13 at 9:42
1

I use following pattern for key and values that should be indexed in key:

 {
"id":"ObjectId",
"key":"error1"
"values":[{"languages":"en", "test":"Error Message 1"},
         {"languages":"fa", "test":"متن خطای شماره 1"}] }
}

object = coleccion.find({"key": "error1"});

view this link Model One-to-Many Relationships with Embedded Documents!

I Reshare This Post

New contributor
j.ghadiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.