Skip to content

Instantly share code, notes, and snippets.

@peterjwest
Created August 6, 2025 13:48
Show Gist options
  • Select an option

  • Save peterjwest/e07133be920d8e571d76a45eb022d635 to your computer and use it in GitHub Desktop.

Select an option

Save peterjwest/e07133be920d8e571d76a45eb022d635 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand
from wagtail.models import Locale, Page
class Command(BaseCommand):
"""
Management command to create translation aliases for existing pages.
This command creates aliases for pages that don't have translation aliases yet.
For use when WAGTAILSIMPLETRANSLATION_SYNC_PAGE_TREE is enabled on an existing project.
Usage:
./manage.py create_translation_aliases
"""
help = "Creates translation aliases for existing pages that don't have them"
def add_arguments(self, parser):
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be done without actually creating aliases",
)
def handle(self, *args, **options):
dry_run = options["dry_run"]
default_locale = Locale.get_default()
other_locales = Locale.objects.exclude(id=default_locale.id)
if not other_locales.exists():
self.stdout.write(
self.style.WARNING("No additional locales found. Make sure you have created locales for translation.")
)
return
default_pages = Page.objects.filter(locale=default_locale).live().public()
total_aliases_created = 0
for locale in other_locales:
self.stdout.write(f"Processing locale: {locale.language_name}")
for page in default_pages:
existing_alias = Page.objects.filter(alias_of=page, locale=locale).first()
if existing_alias:
if not dry_run:
self.stdout.write(f' ✓ Alias already exists for "{page.title}" in {locale.language_name}')
continue
if dry_run:
self.stdout.write(f' Would create alias for "{page.title}" in {locale.language_name}')
else:
try:
alias = page.create_alias(update_locale=locale)
self.stdout.write(
f' ✓ Created alias for "{page.title}" in {locale.language_name} (ID: {alias.id})'
)
total_aliases_created += 1
except Exception as e:
self.stdout.write(
self.style.ERROR(
f' ✗ Failed to create alias for "{page.title}" in {locale.language_code}: {e}'
)
)
if dry_run:
self.stdout.write(self.style.SUCCESS(f"Dry run completed. Would create {total_aliases_created} aliases."))
else:
self.stdout.write(self.style.SUCCESS(f"Successfully created {total_aliases_created} translation aliases."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment