Created
September 2, 2012 09:37
-
-
Save renyi/3596248 to your computer and use it in GitHub Desktop.
Mezzanine Translatable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.