Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not append language code to slug while synchronizing translation tree #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ Contributors
- Nazmul Hasan
- Michael van de Waeter
- Michael van Tellingen
- Mateusz Kleinert
13 changes: 13 additions & 0 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ available sites.
If set to ``True`` the CMS user will only see the tree of the canonical
language, with an ``edit in`` button where they can choose the language to edit
the page in.


``WAGTAILTRANS_APPEND_LANGUAGE_TO_SLUG``
--------------------------

:Default: ``True``

If set to ``False`` and ``WAGTAILTRANS_SYNC_TREE`` set to ``True`` wagtailtrans will
not add language code as a suffix for a slug of newly created page during the translation
trees synchronization.

.. seealso::
The documentation about :ref:`synchronized_trees`
1 change: 1 addition & 0 deletions src/wagtailtrans/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'SYNC_TREE': True,
'LANGUAGES_PER_SITE': False,
'HIDE_TRANSLATION_TREES': False,
'APPEND_LANGUAGE_TO_SLUG': True,
}


Expand Down
1 change: 0 additions & 1 deletion src/wagtailtrans/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.utils.deprecation import MiddlewareMixin
from django.utils.translation import LANGUAGE_SESSION_KEY, check_for_language, get_language_from_path
from django.utils.translation.trans_real import get_languages, get_supported_language_variant

from wagtail.core.models import Site

from .models import Language
Expand Down
7 changes: 5 additions & 2 deletions src/wagtailtrans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ def get_translation_parent(self, language):
)
return translation_parent

def create_translation(self, language, copy_fields=False, parent=None):
def create_translation(self, language, copy_fields=False, parent=None, append_language_to_slug=True):
"""Create a translation for this page. If tree syncing is enabled the
copy will also be moved to the corresponding language tree.

:param language: Language instance
:param copy_fields: Boolean specifying if the content should be copied
:param parent: Parent page instance for the translation
:param append_language_to_slug: Boolean specifying if the language code should be used as a suffix for slug
:return: new Translated page (or subclass) instance

"""
Expand All @@ -274,8 +275,10 @@ def create_translation(self, language, copy_fields=False, parent=None):

if self.slug == self.language.code:
slug = language.code
else:
elif append_language_to_slug:
slug = '%s-%s' % (self.slug, language.code)
else:
slug = self.slug

update_attrs = {
'title': self.title,
Expand Down
3 changes: 2 additions & 1 deletion src/wagtailtrans/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def synchronize_trees(sender, instance, **kwargs):
if not kwargs.get('created') or not getattr(instance, 'language', False) or not is_default_language:
return

append_language_to_slug = get_wagtailtrans_setting('APPEND_LANGUAGE_TO_SLUG')
for lang in other_languages:
instance.create_translation(language=lang, copy_fields=True)
instance.create_translation(language=lang, copy_fields=True, append_language_to_slug=append_language_to_slug)


@disable_for_loaddata
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from tests.factories import language, sites
from tests.factories.pages import HomePageFactory, WagtailPageFactory
from tests.factories.sites import SiteFactory
from wagtailtrans import signals
from wagtailtrans.models import Language, SiteLanguages, TranslatablePage
from wagtailtrans.signals import register_signal_handlers
Expand Down Expand Up @@ -38,6 +39,33 @@ def test_do_not_copy_non_translatable_page(self):

assert TranslatablePage.objects.filter(language=lang, canonical_page=self.last_page).exists()

@override_settings(WAGTAILTRANS_SYNC_TREE=True)
@override_settings(WAGTAILTRANS_APPEND_LANGUAGE_TO_SLUG=False)
def test_do_not_append_language_code_to_slug(self):
"""Test `synchronize_trees()` signal handler."""
en = Language.objects.get(code='en')
fr = language.LanguageFactory(is_default=False, code='fr', position=2)

# Let's create the following structure
# en
# |- subpage
# fr
# |- subpage

site = SiteFactory()
homepage = HomePageFactory.build(language=en, title='en')
site.root_page.add_child(instance=homepage)
subpage = HomePageFactory.build(language=en, title='subpage')
homepage.add_child(instance=subpage)

# For top level nodes slug should be equal to the language code
page = TranslatablePage.objects.filter(language=fr, canonical_page=homepage).get()
assert page.slug == 'fr'

# For the same page, but in a different translation tree slugs should be identical
page = TranslatablePage.objects.filter(language=fr, canonical_page=subpage).get()
assert page.slug == subpage.slug


@pytest.mark.django_db
class TestSignalsLanguagesPerSite:
Expand Down