On Wed, 2008-12-03 at 19:19 -0800, ristretto.rb wrote:
> Hello,
> 
> I have a site that we plan to localize for different countries (all
> English speaking at this point.)  Most of the templates in the site
> will localize fine as they are, but a few will need to be changed.  I
> would like to have one set of templates that is the international
> (default) set, and then only create country specific templates when
> necessary.
> 
> Django template inheritance is excellent, and where I hope to find a
> solution.  What I need is a way for my view methods to forward to
> generic template names, like 'home.html', 'info.html', etc, which
> correspond to the default set of templates, but if any of those
> templates have been overridden with a country specific template, (and
> the user is using the site from that locale,) that country specific
> one, for example 'home_au.html', should be used.

This is one of the lesser-known features of Django and incredibly
useful. You can provide a list of template names that are to be tried in
order and the first one that is found is loaded. You can pass a list of
templates to render_to_response(), since it uses
django.template.loader.render_to_string(), which understands a list as
the first argument. Alternatively, you can use the
django.template.loader.select_loader() call directly to load the
template. You'll have to call render() on the template and put it in an
HttpResponse object yourself in that case, so normally
render_to_response() is going to be more useful. Documentation available
at
http://docs.djangoproject.com/en/dev/ref/templates/api/#the-python-api

In your particular case, you'll arrive at the point where you're ready
to render the template and will know the language code. So pick a
consistent naming scheme and you'll be able to do something like this
(assuming 'locale' contains the locale you want to use) at the end of
your view function:

        return render_to_response(['home_%s.html' % locale,
        'home.html'],
                .... )
        
If the 'home_au.html' template doesn't exist (and, personally, I can't
understand any site that doesn't make the Australian version the
default!), it will load the 'home.html' version.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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