Skip to content

Instantly share code, notes, and snippets.

@renyi
Created September 2, 2012 09:37
Show Gist options
  • Save renyi/3596248 to your computer and use it in GitHub Desktop.
Save renyi/3596248 to your computer and use it in GitHub Desktop.
Mezzanine Translatable
from django.contrib import admin
from mezzanine.conf import settings
from mezzanine.core.admin import TabularDynamicInlineAdmin
if "mezzanine.pages" in settings.INSTALLED_APPS:
from mezzanine.pages.models import RichTextPage, Link
from mezzanine.pages.admin import PageAdmin, LinkAdmin
from models import TransRichTextPage, TransLinkPage
#
# Richtext
#
class TransInline(TabularDynamicInlineAdmin):
model = TransRichTextPage
fields = ("lang", "title", "content")
class TransPageAdmin(PageAdmin):
inlines = (TransInline,)
admin.site.unregister(RichTextPage)
admin.site.register(RichTextPage, TransPageAdmin)
#
# Link
#
class TransLinkInline(TabularDynamicInlineAdmin):
model = TransLinkPage
fields = ("lang", "title", "slug")
class TransLinkAdmin(LinkAdmin):
inlines = (TransLinkInline,)
admin.site.unregister(Link)
admin.site.register(Link, TransLinkAdmin)
if "mezzanine.forms" in settings.INSTALLED_APPS:
from mezzanine.forms.models import Form, Field
from mezzanine.forms.admin import FormAdmin, FieldAdmin
from models import TransField, TransForm
#
# Form
#
class TransFormInline(TabularDynamicInlineAdmin):
model = TransForm
fields = ("lang", "title", "content", "button_text", "response")
class TransFormAdmin(FormAdmin):
inlines = (FieldAdmin, TransFormInline)
admin.site.unregister(Form)
admin.site.register(Form, TransFormAdmin)
class TransFieldInline(TabularDynamicInlineAdmin):
model = TransField
fields = ("lang", "original", "label", "choices", "default", "help_text")
class TransFieldAdmin(admin.ModelAdmin):
inlines = (TransFieldInline, )
fields = ("label", "choices", "default", "help_text")
admin.site.register(Field, TransFieldAdmin)
#
# Gallery
#
if "mezzanine.galleries" in settings.INSTALLED_APPS:
from mezzanine.galleries.models import Gallery, GalleryImage
from mezzanine.galleries.admin import GalleryAdmin, GalleryImageInline
from models import TransGallery, TransGalleryImage
class TransGalleryInline(TabularDynamicInlineAdmin):
model = TransGallery
fields = ("lang", "title", "content", )
class TransGalleryAdmin(GalleryAdmin):
inlines = (GalleryImageInline, TransGalleryInline, )
admin.site.unregister(Gallery)
admin.site.register(Gallery, TransGalleryAdmin)
from django.db import models
from django.utils.translation import ugettext_lazy as _
from mezzanine.conf import settings
from mezzanine.core.models import Slugged, MetaData, Displayable, Orderable, RichText
from mezzanine.core.fields import RichTextField
class Translatable(models.Model):
lang = models.CharField(max_length=5, choices=settings.LANGUAGES)
class Meta:
abstract = True
ordering = ("lang",)
if "mezzanine.pages" in settings.INSTALLED_APPS:
from mezzanine.pages.models import RichTextPage, Link
class TransRichTextPage(Translatable, RichText, Slugged):
translation = models.ForeignKey(RichTextPage, related_name="translation")
class Meta:
verbose_name = _("Translated Page")
verbose_name_plural = _("Translated Pages")
ordering = ("lang",)
unique_together = ("lang", "translation")
class TransLinkPage(Translatable, Slugged):
translation = models.ForeignKey(Link, related_name="translation")
class Meta:
verbose_name = _("Translated Link")
verbose_name_plural = _("Translated Links")
ordering = ("lang",)
unique_together = ("lang", "translation")
if "mezzanine.forms" in settings.INSTALLED_APPS:
from mezzanine.forms import fields
from mezzanine.forms.models import Form, FieldManager, Field
class TransForm(Translatable, RichText, Slugged):
translation = models.ForeignKey(Form, related_name="translation")
button_text = models.CharField(_("Button text"), max_length=50, default=_("Submit"))
response = RichTextField(_("Response"))
class Meta:
verbose_name = _("Translated Form")
verbose_name_plural = _("Translated Forms")
ordering = ("lang",)
unique_together = ("lang", "translation")
class TransField(Translatable):
translation = models.ForeignKey(Field, related_name="translation")
original = models.CharField(_("Label (Original)"), max_length=settings.FORMS_LABEL_MAX_LENGTH)
label = models.CharField(_("Label"), max_length=settings.FORMS_LABEL_MAX_LENGTH)
choices = models.CharField(_("Choices"), max_length=1000, blank=True,
help_text=_("Comma separated options where applicable. "
"If an option itself contains commas, surround the option with `backticks`."))
default = models.CharField(_("Default value"), blank=True,
max_length=settings.FORMS_FIELD_MAX_LENGTH)
help_text = models.CharField(_("Help text"), blank=True, max_length=100)
class Meta:
verbose_name = _("Translated Field")
verbose_name_plural = _("Translated Fields")
ordering = ("lang",)
if "mezzanine.galleries" in settings.INSTALLED_APPS:
from mezzanine.galleries.models import Gallery, GalleryImage
class TransGallery(Translatable, Slugged, RichText):
translation = models.ForeignKey(Gallery, related_name="translation")
class Meta:
verbose_name = _("Translated Gallery")
verbose_name_plural = _("Translated Galleries")
ordering = ("lang",)
class TransGalleryImage(Translatable, Slugged):
translation = models.ForeignKey(GalleryImage, related_name="translation")
description = models.CharField(max_length=1000, blank=True)
class Meta:
verbose_name = _("Translated Image")
verbose_name_plural = _("Translated Images")
ordering = ("lang",)
{# page.html #}
{% extends "base.html" %}
{% load i18n mezzanine_tags keyword_tags translate_tags %}
{% block meta_title %}
{% with translated_page=page|get_object_translation %}
{{ translated_page.title }}
{% endwith %}
{% endblock %}
{% block meta_keywords %}{% metablock %}
{% keywords_for page as keywords %}
{% for keyword in keywords %}
{% if not forloop.first %}, {% endif %}
{{ keyword }}
{% endfor %}
{% endmetablock %}{% endblock %}
{% block meta_description %}{% metablock %}
{{ page.description }}
{% endmetablock %}{% endblock %}
{% block title %}
{% with translated_page=page|get_object_translation %}
{{ translated_page.title }}
{% endwith %}
{% endblock %}
{% block main %}
{% endblock %}
{# richtextpage.html #}
{% extends "pages/page.html" %}
{% load i18n mezzanine_tags translate_tags %}
{% block main %}
{{ block.super }}
{% with translated_page=page|get_object_translation %}
{{ translated_page.richtextpage.content|safe }}
{% endwith %}
{% endblock %}
from django import template
from django.utils import translation
register = template.Library()
@register.filter
def get_object_translation(obj):
# get current language
lang = translation.get_language()
try:
# returns object with current translation
for i in obj.translation.all():
if i.lang == lang:
return i
except:
pass
# returns object without translation
return obj
@nkeilar
Copy link

nkeilar commented Mar 6, 2013

@renyi just wondering if this is the best approach looking back. Have you been happy with it? Why didn't you go with something like django-modeltranslation? Any other tips on language selection in templates, or url patterns? Sorry for playing 20 questions!

@hypertexthero
Copy link

I'm trying to use this together with django-localeurl. I can access the various URLs and see messages in .po files translated successfully, but the actual site content translations do not appear (only the default English does).

Any idea how to connect this code with django-localeurl? Perhaps in line 9 of translate_tags.py ?

@hypertexthero
Copy link

I managed to get it to work by changing the with blocks in page.html and richtextpage.html to the following:

page.html:

{% with page.richtextpage|get_object_translation as translated_page %}
    {{ translated_page.title|safe }}
{% endwith %}

richtextpage.html:

{% with page.richtextpage|get_object_translation as translated_page %}
    {{ translated_page.content|richtext_filter|safe }}
{% endwith %}

@korsvanloon
Copy link

I tried to follow hypertexthero's path and I kind of succeeded with django-localeurl, but the admin interface already uses localization and now admin urls look lie /en/en/admin/. Which makes al the links broken (they become /en/en/en/admin/.... Does anyone know how to solve this?

@renyi
Copy link
Author

renyi commented Aug 19, 2014

@madteckhead, sorry for late reply. This was a quick hack I threw together for a project I was working on. It may not work with the latest mezzanine/django. A long standing discussion is here, for what Mezzanine will be doing for translation, stephenmcd/mezzanine#106.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment