How to translate database content?

623 views
Skip to first unread message

Cody Scott

unread,
Apr 16, 2013, 12:15:40 PM4/16/13
to django...@googlegroups.com
I want the end user of the site to be able to view the site in different languages.

I have marked the static text in my templates for translation. I have not marked the verbose_names because they are used in the admin which the end user will not have access to.

But I just realized that the actually content in the database needs to be translated. If it was a blog site the blog posts which are stored in the database would also need to be translated.

How do I mark them for translation?

How can I see the site in a different language. In the tutorial is says you can see the admin section in a different language how do I do that?

Daniel Krol

unread,
Apr 19, 2013, 5:02:57 PM4/19/13
to django...@googlegroups.com
Oh, just to be clear, the hack involves putting the content into the template, and marking it for translation.

On Friday, April 19, 2013 1:57:44 PM UTC-7, Daniel Krol wrote:
There isn't anything built into Django to translate the content of models. This is because the GNU gettext system that Django uses for translation stores the original->translation mappings in a "compiled" file which only changes between restarts of the server. That means anything you dynamically create in the database cannot be translated with this system, at least without a sort of a hack.

And the hack is this: If you have access to the data from the live database from your development environment, you could theoretically dump it (the actual full text verbatim, not just a variable containing the content), to a dummy template file. Then, makemessages will pick up that text and put it into the translation files. *then* you can translate it, and say {% trans model_instance.field %}. Where I work, we've done this for very small pieces of data (category names, of which there are less than 10). It's manageable at this point. But with full blog posts, unless you automate some sort of system, it's probably way too big a hack to be worth it. (You'd have to make sure your spacing was 100% correct, too.)

No, what I recommend is to create a model in the database that serves as the translated version of all the specific models whose content you want to translate. For instance, you want BlogPostTranslation, with an FK to BlogPost, and also a locale. Then in your view, you select the appropriate translation to show based on the locale in the request (request.LANGUAGE_CODE).

Since this is somewhat of a common concern, there are libraries out there that facilitate this. I won't list them because I have no recommendations on the matter (I have not tried any personally), your own Google search will be as good as mine. But look for things along the line of "django model translation".

Hope this helps.

Cody Scott

unread,
Apr 19, 2013, 5:35:28 PM4/19/13
to django...@googlegroups.com
I installed dbgettext and it allows you to mark model fields and then export them with dbgettext_export. Then when you run django-admin.py makemessages it includes the content in the .po file.

Then you use django-admin.py compilemessages like normal.

The only problem I am having is that only three words are being translated.

Home (which was not from the database), Yes and No.

Also I keep getting switched to the english version of the site with the url having /en/ in the beginning while I set it to /ja/


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/upzy6ohwlKc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages