Skip to content

Instantly share code, notes, and snippets.

@wullerot
Last active October 14, 2017 09:19
Show Gist options
  • Save wullerot/9fe3151101e57a9ee6fadb3cababb619 to your computer and use it in GitHub Desktop.
Save wullerot/9fe3151101e57a9ee6fadb3cababb619 to your computer and use it in GitHub Desktop.
django-modeltranslation-language-tabs
from __future__ import unicode_literals
from django.conf import settings
class LanguageTabsMixin(object):
change_form_template = 'admin/modeltranslation/change_form.html'
def change_view(self, request, object_id, form_url='', extra_context=None):
context = extra_context or {}
context['tab_languages'] = settings.LANGUAGES
return super(LanguageTabsMixin, self).change_view(
request,
object_id=object_id,
form_url=form_url,
extra_context=context
)
def add_view(self, request, form_url='', extra_context=None):
context = extra_context or {}
context['tab_languages'] = settings.LANGUAGES
return super(LanguageTabsMixin, self).add_view(
request,
form_url=form_url,
extra_context=context
)
{% extends 'admin/change_form.html' %}
{% load i18n admin_static admin_modify cms_admin %}
{% block extrahead %}{{ block.super }}
<script src="{% static 'admin/modeltranslation/language_tabs.js' %}" type="text/javascript"></script>
<link href="{% static 'admin/modeltranslation/language_tabs.css' %}" rel="stylesheet" type="text/css">
{% endblock %}
{% block field_sets %}
<div id="page_form_lang_tabs" class="languagetabs_mixin">
{% for lang_code, lang_name in tab_languages %}
<input type="submit" class="language_button{% if forloop.first %} selected{% endif %}" value="{{ lang_name }}" data-language="{{ lang_code }}">
{% endfor %}
<div class="lang_tabs_line"></div>
</div>
{{ block.super }}
{% endblock %}
/* assumes djangocms-admin-style is installed */
.pseudo-hidden {
position: absolute;
left: -9000000px;
top: 0;
}
var ModelTranslation = (function( $ ) {
'use strict';
$('document').ready(init);
var $buttons;
var $menu;
var fields = {};
var now_lang = 'de';
function init() {
$menu = $('#page_form_lang_tabs.languagetabs_mixin');
$buttons = $('input', $menu);
prepare_form();
$buttons.bind({ click: change_language });
};
function change_language(e) {
e.preventDefault();
var $button = $(this);
var lang = $button.data('language');
$buttons.removeClass('selected');
$button.addClass('selected');
fields[now_lang].addClass('pseudo-hidden');
fields[lang].removeClass('pseudo-hidden');
now_lang = lang;
};
function prepare_form() {
for( var i = 0; i < $buttons.length; i++ ) {
var $button = $( $buttons[i] );
var lang = $button.data('language');
fields[lang] = $('.form-row[class$="_' + lang + '"]');
if ( $button.hasClass('selected') ) {
now_lang = lang;
} else {
// TODO delay until ckeditor is loaded
fields[lang].addClass('pseudo-hidden');
}
}
};
})( django.jQuery );
@benzkji
Copy link

benzkji commented Oct 14, 2017

! perfect !

@benzkji
Copy link

benzkji commented Oct 14, 2017

note: if used for a djangocms plugin edit dialog, language could be preselected base on GET[cms_path], so one edits directly the language he's in...see my fork for a proof of concept.

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