Hi!

Le Sun, 27 Jan 2008 09:03:25 -0800 (PST), Emil
<[EMAIL PROTECTED]> 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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to