/kb

personal knowledgebase

Archive for the ‘django’ tag

Translation of Django Core

without comments

cd django_trunk
svn update
# Generate .po files:
bin/django-admin.py makemessages -l nn 
bin/django-admin.py makemessages -d djangojs -l nn
cd conf/locale/nn/LC_MESSAGES

Edit .po files. Search for text string fuzzy and empty double quotes (“”) to find untranslated strings.

Finally, create diffs:

svn diff django.po > nn-django-translation-update.diff
svn diff djangojs.po > nn-djangojs-translation-update.diff

Written by hgrimelid

August 7th, 2010 at 9:31 pm

Posted in Programming

Tagged with ,

Translation of Django Apps

with 2 comments

I recently created a Django app that needed translation of the Django administration. It took some googling and messing around to make it work, so this quick and dirty walkthrough will hopefully be helpful for others.

Here’s how I did it:

Add translations for all model fields:

class Stuff(models.Model):
    title = models.CharField(_('title'), max_length=255)
    the_other_model = model.ForeignKey('othermodel', verbose_name=_('the other model'), verbose_name_plural=_('the other models'))
 
    class Meta:
      verbose_name = _('stuff')
      verbose_name_plural = _('stuffs')

Then the language (.po) files must be created:

mkdir locale
python manage.py makemessages -l LANG_CODE

Then edit your language files in locale/_LANGUAGE_/LC_MESSAGES/django.po. When you’ve finished editing the language file must be compiled:

python manage.py compilemessages

If you want to translate app names as well, you can add something like this to your project __init__.py:

from django.utils.translation import ugettext_lazy as _
 
_(u'auth')
_(u'configuration')
_(u'sites')
_(u'appname')
_(u'Auth')
_(u'Configuration')
_(u'Sites')
_(u'Appname')

This feels like a dirty hack but I couldn’t find any other way to do it.

Source: Django documentation – Internationalization and localization, and some Google Groups thread that I can’t find right now, for the app name translation.

Written by hgrimelid

March 4th, 2010 at 3:50 pm

Posted in Programming

Tagged with ,

Chapter 7 in “Python Web Development with Django” – some notes

with 2 comments

I am currently in the process of learning Django and have just finished chapter 7 in Python Web Development with Django. Here are some notes on how I got it working (except for the index page which still isn’t completely as expected) on an ubuntu hardy installation.

The photo gallery example assumes an apache2 + mod_python setup. I didn’t really find it straightforward to get this running from the description in the appendix, so here is how I got it to work (after installation of apache2 and mod_python, of course):

In /etc/apache2/sites-available/default, change lines:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

To:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig
                Order allow,deny
                allow from all

                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>

Then I did the changes in /etc/apache2/httpd.conf, but slightly different from the book by adding a PythonOption django.root (didn’t work for me with the setup in the book):

<Location "/gallery/">
      SetHandler python-program
      PythonHandler django.core.handlers.modpython
      SetEnv DJANGO_SETTINGS_MODULE gallery.settings
      PythonOption django.root /gallery
      PythonDebug On
      PythonPath "['/path/to/django/root', '/home/user/django-stuff'] + sys.path"
</Location>

With an additional entry for media files:

<Location "/gallery/media">
      SetHandler none
</Location>

Now, I created a directory /var/www/gallery and a /var/www/gallery/media like explained in “Preparing for File Uploads” and edited settings.py:

MEDIA_ROOT = '/var/www/gallery/media/'
MEDIA_URL = 'http://localhost/gallery/media/'
ADMIN_MEDIA_PREFIX = 'http://localhost/gallery/media/admin/'

Where admin is a symbolic link (/var/www/gallery/media/admin) to the admin media directory in the Django installation. The two last entries where not mentioned in the book, but they made it work…

With the settings described above and the PythonOption django.root in httpd.conf, I didn’t need the section about DRY URLs. And it worked – as opposed to the stuff in the book.

With all the configuration stuff in place I typed the code outlined in the chapter and to begin with I couldn’t see any photos, except for the thumb_url in items_list.html (typo: items_listing.html in the last paragraph page 175). I managed to get it working by changing some lines in items_detail.html and photos_detail.html though:

22                 <img src="{{ photo.image.thumb_url }}" />

instead of

22                 <img src="{{ photo.get_image_thumb_url }}" />

and

8 <img src="{{ object.image.url }}" />

instead of:

8 <img src="{{ object.get_image_url }}" />

The alternatives that worked seems more intuitive to me.

I also imported list_detail from django.views.generic in urls.py, but am not sure if this was needed. The photo gallery seems to work fine except for the thumbnails in index.html. I’ve noticed that the code in urls.py for that template is different from the others, using simple.direct_to_template, but I haven’t managed getting that part to work.

Thanks to this thread for getting mod_python up and running: Python and Apache2

Written by Morten Wergeland Hansen

March 8th, 2009 at 8:08 pm

Posted in Programming

Tagged with , , , ,