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?
1
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.
3
I use following pattern for key and values that should be indexed in key:
{
"id":"ObjectId",
"key":"error1"
"values":[{
"lang":"en",
"value":"Error Message 1"
},
{
"lang":"fa",
"value":"متن خطای شماره 1"
}]
}
and Use This Code in C#
object = coleccion.find({"key": "error1"});
view this link Model One-to-Many Relationships with Embedded Documents!
I Reshare This Post