I am having the same problem too. I have not gone that far to
implementing the details of templates, but my planned solution is to use
just one template without any language dependent switching. Instead, I
will write some code to extract the relevant fields (title_en or
title_sv as in your example) for user selected language and supply that
as the context to the template. Just want to give an alternative approach.
Wanrong
Le Sun, 27 Jan 2008 09:03:25 -0800 (PST), Emil
<bjorklu...@gmail.com> a écrit :
> Hi!
>
> I'm currently working on a couple of sites where basically all content
> is to be available in two languages. I haven't used django-
> multilingual or anything readymade, but simply created double fields
> in my models for all content in the db that needs to be available in
> both languages. For example, a post with a title might have "title_en"
> and "title_sv".
>
[...]
>
> {% switch LANGUAGE_CODE %}
> {% case "en" %}{{ title_en }}{% endcase %}
> {% case "sv" %}{{ title_sv }}{% endcase %}
> {% endswitch %}
Depending on your model, you can do this switch statement inside a
model method.
Something like this :
==============================================
from django.db import models
from django.conf import settings
class Post(models.Model):
title_en = ...
title_sv = ...
def title(self):
if settings.LANGUAGE_CODE == 'en':
return self.title_en
else:
return self.title_sv
==============================================
... and so, your only need to specify "{{ post.title }}" in your
template.
However, it can be a bit verbose if you have several fields with the
same scheme as "title_*".
Or maybe, if you like dark magic art, you can try to set up a
__getattr__ method in your Post class, like that :
=============================
def __getattr__(self, attribute):
lang = settings.LANGUAGE_CODE
return getattr(self, "%s_%s" % (attribute, lang))
=============================
It seems to work :
>>> from django.conf import settings
>>> p = m.Post.objects.all()[0]
>>> settings.LANGUAGE_CODE
'en'
>>> p.title
u'en title'
>>> settings.LANGUAGE_CODE = 'sv'
>>> p.title
u'sv title'
>>> settings.LANGUAGE_CODE = 'en'
>>> p.title
u'en title'
... but you might want to refine this, because it's a bit hacky and it
explodes if you specify an non-existing attribute (recursives calls, and
the like).
Or, if you are not-so-evil, you can try to make a template filter,
which takes in parameters a field, and it will try to do this (instead
of the tricky use of getattr in the Model class).
Something like :
==================================
from django.conf import settings
from django.template.defaultfilters import stringfilter
from django import template
register = template.Library()
@register.filter
@stringfilter
def translate(obj, field):
return getattr(obj, "%s_%s" % (field, settings.LANGUAGE_CODE))
==================================
And you should be able to use it like that in your templates :
{{ post|translate:"title" }}
But it's not very pretty neither.
The first proposition is the cleaner, but it's not really dry.
The second one can be great, but you can shoot yourself in the foot
with __getattr__. Be careful.
I hope it might help (or give ideas ...)
- Jonathan