diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d1ae82 --- /dev/null +++ b/README.md @@ -0,0 +1,113 @@ +# Wagtail modeltranslation +(based on https://github.com/deschler/django-modeltranslation) + +Simple app containing a mixin model that integrates modeltranslation +(https://github.com/deschler/django-modeltranslation) into wagtail panels system. + +![alt tag](https://github.com/infoportugal/wagtail-modeltranslation/blob/master/screenshot.png?raw=true) + +## Quick start + +1. Make sure you have django-modeltranslation installed: + + pip install django-modeltranslation + +2. Add "wagtail_modeltranslation" to your INSTALLED_APPS setting like this: + + INSTALLED_APPS = ( + ... + 'wagtail_modeltranslation', + 'modeltranslation', + ... + ) + +3. Use TranslationMixin in order to integrate django-modeltranslation with Wagtail admin. ** IMPORTANT: ** make sure that TranslationMixin is declared before Page class on model inheritance. Like following: + + from wagtail_modeltranslation.models import TranslationMixin + class FooModel(TranslationMixin, Page): + foo = models.CharField() + FooModel.panels = [...] + +4. Visit django-modeltranslation for documentation on how to implement translation fields: http://django-modeltranslation.readthedocs.org/en/latest/ + +5. In order to update pages url_path field use **"set\_translation\_url\_paths"** instead of original "set\_url\_paths" + +6. Use **"change_lang"** template tag to fetch translated urls based on translated slugs: + + {% load modeltranslation %} + + +## Release Notes + +## v0.1.5 + +- Fixed required fields related bug + +## v0.1.4 + +- Support for wagtailsearch and wagtailsnippets + +## v0.1.3 + +- Support for translated inline panels #8: https://github.com/infoportugal/wagtail-modeltranslation/issues/8 + +## v0.1.2 + +- Fixed Problem updating field with null value #6: https://github.com/infoportugal/wagtail-modeltranslation/issues/6 + +## v0.1.1 + +- Fixed url_path issue caused by a browser with language different from settings.LANGUAGE_CODE + +## v0.1 + +- Minor release working but lacks full test coverage. +- Last version had required fields validation problems, now fixed. + +## v0.0.9 + +- Fixed issue that causes duplicated translation fields, preventing auto-slug from working properly. + +## v0.0.8 + +- Fixed issue related to deleting a non existing key on PAGE_EDIT_HANDLER dict + +## v0.0.7 + +- Fixed set_url_path() translatable model method + +### v0.0.6 + +- Fixed js issue related to auto-generating slugs + +### v0.0.5 + +- Now using django-modeltranslation 0.9.1; +- Fixed problem related to slug field fallbacks; + +### v0.0.4 + +** IMPORTANT: ** make sure that TranslationMixin comes before Page class on model inheritance + +- Fix enhancement #1: url_path translation field +- Now includes a template tag that returns current page url to corresponding translated url +- New management command to update url_path translation fields - **set\_translation\_url\_paths** + +### v0.0.3 + +- New methods; +- Now supports required fields; +- Fixed issue related to browser locale; + +### v0.0.2 + +- Prepopulated fields now works for translated fields (title and slug) diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..c0a6c55 Binary files /dev/null and b/screenshot.png differ diff --git a/wagtail_modeltranslation/models.pyc b/wagtail_modeltranslation/models.pyc new file mode 100644 index 0000000..4c8e1a3 Binary files /dev/null and b/wagtail_modeltranslation/models.pyc differ diff --git a/wagtail_modeltranslation/static/wagtail_modeltranslation/js/translated_slugs.js b/wagtail_modeltranslation/static/wagtail_modeltranslation/js/translated_slugs.js new file mode 100644 index 0000000..5626714 --- /dev/null +++ b/wagtail_modeltranslation/static/wagtail_modeltranslation/js/translated_slugs.js @@ -0,0 +1,15 @@ +$(document).ready(function() { + $.each(langs, function(idx, lang_code){ + $('#id_title_'+lang_code).on('focus', function() { + $('#id_slug_'+lang_code).data('previous-val', $('#id_slug_'+lang_code).val()); + $(this).data('previous-val', $(this).val()); + }); + + $('#id_title_'+lang_code).on('keyup keydown keypress blur', function() { + if ($('body').hasClass('create') || (!$('#id_slug_'+lang_code).data('previous-val').length || cleanForSlug($('#id_title_'+lang_code).data('previous-val')) === $('#id_slug_'+lang_code).data('previous-val'))) { + // only update slug if the page is being created from scratch, if slug is completely blank, or if title and slug prior to typing were identical + $('#id_slug_'+lang_code).val(cleanForSlug($('#id_title_'+lang_code).val())); + } + }); + }); +}); \ No newline at end of file diff --git a/wagtail_modeltranslation/templatetags/modeltranslation.py b/wagtail_modeltranslation/templatetags/modeltranslation.py new file mode 100644 index 0000000..8acd238 --- /dev/null +++ b/wagtail_modeltranslation/templatetags/modeltranslation.py @@ -0,0 +1,41 @@ +# coding: utf-8 + +import re + +from django import template +from django.core.urlresolvers import resolve +from django.utils.translation import activate, get_language +register = template.Library() + + +# CHANGE LANGUAGE +@register.simple_tag(takes_context=True) +def change_lang(context, lang=None, *args, **kwargs): + current_language = get_language() + + if 'request' in context and lang and current_language: + request = context['request'] + match = resolve(request.path) + non_prefixed_path = re.sub(current_language+'/', '', request.path) + + # means that is an wagtail page object + if match.url_name == 'wagtail_serve': + path_components = [component for component in non_prefixed_path.split('/') if component] + page, args, kwargs = request.site.root_page.specific.route(request, path_components) + + activate(lang) + translated_url = page.url + activate(current_language) + + return translated_url + elif match.url_name == 'wagtailsearch_search': + path_components = [component for component in non_prefixed_path.split('/') if component] + + translated_url = '/' + lang + '/' + path_components[0] + '/' + if request.GET: + translated_url += '?' + for key, value in request.GET.iteritems(): + translated_url += key + '=' + value + return translated_url + + return '' diff --git a/wagtail_modeltranslation/tests.py b/wagtail_modeltranslation/tests.py new file mode 100644 index 0000000..57d631c --- /dev/null +++ b/wagtail_modeltranslation/tests.py @@ -0,0 +1 @@ +# coding: utf-8