tips for localize syncronization when importing
Importing and Synchronizing Pages with Wagtail and Wagtail Localize
Problem statement
One of our clients, with an extensive database of translated articles, wants to switch to Wagtail. Obviously, copying and pasting all that content manually is not an option. Moreover, the customer wants to use Wagtail Localize to perform machine translations. However, simply transferring the content to the new database did not prove sufficient. The migration turned out to be a bit more complex, especially since not following the correct procedure could mean missing out on the Wagtail Synchronize option for translations. Below we share the steps needed to get this process right.
The process of page import and translation with Wagtail Localize
When importing and translating pages in Wagtail, everything starts with an original page, for example in Dutch. This original_page forms the basis of your content and the basis for creating translations to other languages.
Step 1: Creation of translations
The translate_object function allows you to generate translations in a simple way. This function accepts two parameters:
- The original page you created earlier, for example, in Dutch.
- A list of languages (or locales) into which you want to translate the original page.
This function ensures that the original content is copied to the new pages, this with the original content.
translate_object(original_page, locales)
Step 2: Linking the TranslationSource
After creating translations, it is important to obtain the TranslationSource. This model, part of Wagtail Localize, manages the relationship between the original page and the translations. You can retrieve the TranslationSource as follows these were already created in the translate_object:
translation_source, created = TranslationSource.get_or_create_from_instance(original_page)
translation_source is crucial to access the translation synchronization status.
Step 3: Disable synchronization
Once you have access to the TranslationSource, you can disable synchronization. You do this by finding all linked translations through the Translation model and setting the Enabled property to False:
Translation.objects.filter(source=translation_source).update(enabled=False)
Disabling synchronization allows you to automatically update the content in the translated pages without automatically overwriting the content in the original page.
Practical example
Suppose you have created a page in Dutch. You want to add translations in French, English and German. The process would look like this:
- Start with the original page. It contains all the basic information and is ready to be translated.
- Run translate_object. This creates the translations in the desired languages.
- Get the TranslationSource. This manages the link between the original page and the translations.
- Turn off synchronization. This allows you to edit translated content without restrictions.
from wagtail_localize.models import TranslationSource, Translation
from wagtail.core.models import Locale
def create_wagtail_localize_translations(original_page: Page):
locales = Locale.objects.all()
translate_object(original_page, locales)
translation_source, created == TranslationSource.get_or_create_from_instance(original_page)
Translation.objects.filter(source=translation_source).update(enabled=False)
While the process may seem a bit cumbersome at first, this approach provides the flexibility to translate and customize pages in a controlled manner. By investing time in a structured workflow, as described above, you can create consistent and quality multilingual content without unexpected synchronization issues.