django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

Sugestion: Use jsonfield for translation

Open ebertti opened this issue 9 years ago • 5 comments

In Django 1.9 using Postgresql, we have a extra field, JsonField

https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#jsonfield

Or can use:

https://github.com/bradjasper/django-jsonfield/

This way, you can use a dynamic translation for each field, just store the language key and the value, like:

{"en": "Title", "pt": "Titulo", "it": "Titolo"}

So, I think you can store every thing on just one field. And don't need to create a lot of field for each language, or when you change a language, you need to migrate the model.

Using Postgre field you can query this fiels like another text field:

class Article(models.Model):
    title = models.Charfield()
    title_translated = modeltranslation.Charfield(for="title")

query example:

>>> Article.objects.filter(title_translated__en__contains="Title").count()
>>> 1

Works fine!

Ofcourse, this example need to be adapt for modeltranslation API.

ebertti avatar Jan 21 '16 00:01 ebertti

+1, came here to suggest this solution and found out somebody beat me to it. Using a JSONField would remove the biggest obstacle when using MANY languages (being that the model gets insanely big).

dyve avatar Jan 26 '16 17:01 dyve

:+1:

peterlauri avatar Jan 28 '16 17:01 peterlauri

:+1:

SalahAdDin avatar Feb 05 '16 06:02 SalahAdDin

Well, even HStoreField would suit this case. However, as we aim to maintain compatibility with MySQL and SQLite databases as well, it's unlikely that we would use this feature. Maybe in some experimental branch.

zlorf avatar Feb 05 '16 09:02 zlorf

It would be difficult to create two strategies? One for postgre and another for others? Like the gis integrations do?

ebertti avatar Feb 05 '16 12:02 ebertti