Replies: 2 comments 1 reply
-
Yeah sure - this should work with any wagtail-localize backend to translate all I'll walk you through the basics - First we must get the Now we can fetch the translations for that source - Loop over the translations a rough sketch, the fields to filter should be correct; not paying attentions to methods as I'm explaining this. for p in page.objects.all.specific:
source = TranslationSource.objects.get_for_instance(p)
for translatable in source.translations.all():
po_file = translatable.export_po
# ... translating here
translatable.import_po EditThis seems like something I'd like to use myself in the future so I built the main bit of functionality. def translate_pages(self, data = None, force_translate = False):
if data:
page_models = [apps.get_model(model) for model in data]
else:
page_models = [Page]
for PageClass in page_models:
for page in PageClass.objects.all():
self.stdout.write(f"Translating {page}")
try:
source: TranslationSource = TranslationSource.objects.get_for_instance(page)
if not source:
self.stderr.write(f"No translation source found for {page}")
continue
except (TranslationSource.DoesNotExist, TranslatableObject.DoesNotExist):
self.stderr.write(f"No translation source found for {page}")
continue
source_locale: Locale = source.locale
translations: list[Translation] = source.translations.all()
for translation in translations:
po_file = translation.export_po()
if not po_file:
self.stderr.write(f"No PO file found for {translation}")
continue
self.stdout.write(f"Translating {po_file} from {source_locale} to {translation.target_locale}")
self.translate_po(
source_locale=source_locale,
target_locale=translation.target_locale,
po=po_file,
force_translate=force_translate,
)
self.stdout.write(f"Saving PO file to page {page}")
warnings = translation.import_po(
po=po_file,
delete=False,
user=None,
translation_type="machine",
tool_name=self.translator.display_name,
)
if warnings:
for warning in warnings:
self.stderr.write(f"Warning received importing {po_file}: {warning}") You should also edit your command's
Command codeHere is the code I used for the command - again, you should edit this to fit your use-case. The method you should be most interested in from the command-code is probably
|
Beta Was this translation helpful? Give feedback.
-
Hey! looks great!!! You sure did your research on how this package works, great! :) Here is an example of a standalone script for translating pages, you can literally just import this into your command and use it, very easy to follow too! We'd want to pass as much as it off as we can to wagtail_localize itself. What if there's an update for example? Anyways, here you go:
|
Beta Was this translation helpful? Give feedback.
-
I'm trying to automate the translation of a Wagtail website with hundreds of pages. My goal is to create a Django command to translate all the pages with deepl. I can't find any documentation about it. Anyone as ever encountered the same issue ?
I'm using Wagtail 5.2 and wagtail-localize 1.7
Beta Was this translation helpful? Give feedback.
All reactions