def _download_and_apply_translations()

in src/wagtail_localize_smartling/sync.py [0:0]


def _download_and_apply_translations(job: "Job") -> None:
    """
    Download the translated files from a Smartling job and apply them
    """

    logger.info("Downloading and importing translations for job %s", job)

    _translations_imported = []

    with client.download_translations(job=job) as translations_zip:
        for zipinfo in translations_zip.infolist():
            # Filenames are of the format "{localeId}/{fileUri}"
            smartling_locale_id, file_uri = zipinfo.filename.split("/")

            if file_uri != job.file_uri:
                raise FileURIMismatch(
                    f"File URI mismatch: expected {job.file_uri}, got {file_uri}"
                )

            wagtail_locale_id = utils.format_wagtail_locale_id(smartling_locale_id)
            try:
                translation: Translation = job.translations.get(
                    target_locale__language_code=wagtail_locale_id
                )
            except job.translations.model.DoesNotExist:
                logger.info(
                    "Translation not found for locale %s, skipping", wagtail_locale_id
                )
                continue

            with translations_zip.open(zipinfo) as f:
                po_file = polib.pofile(f.read().decode("utf-8"))
                translation.import_po(po_file)
                individual_translation_imported.send(
                    sender=job.__class__,
                    instance=job,
                    translation=translation,
                )
                logger.info("Imported translations for %s", translation)
                _translations_imported.append(translation)

    if _translations_imported:
        translation_import_successful.send(
            sender=job.__class__,
            instance=job,
            translations_imported=_translations_imported,
        )