Skip to main content

XLIFF Import and Export for the Django CMS

Project description

djangocms-xliff

Tests PyPI

XLIFF (XML Localization Interchange File Format) is an XML-based format created to standardize the way localizable data are passed between and among tools during a localization process.

With djangocms-xliff it is possible to export all text objects from a page into an XLIFF-compatible file and re-import the file at the end of the translation process.

Migration to django-cms 5

If you are using djangocms-xliff with django-cms version 3 or lower use the v1 release of this package:

pip install djangocms-xliff==1.*.*

We are not planning to support this package for django-cms version 3 or lower

Please upgrade to a v2 release if you are using django-cms version 5 or higher

Installation

Before the installation you need to make sure, that your localization / internalization are set up properly for Django and Django-CMS

Setup

djangocms-xliff is available on PyPI:

$ pip install djangocms-xliff

Add djangocms_xliff to your INSTALLED_APPS.

INSTALLED_APPS = (
    ...,
    "djangocms_xliff"
)

Add the views for djangocms_xliff to your urls.py

urlpatterns = [
    path("xliff/", include("djangocms_xliff.urls"))
]

Documentation

To make the process fail-safe there are some Django CMS related restrictions:

  • You can only import the file to the same page and language that you exported from before.
  • It is not possible to export a file for one language and import it to another language.
  • It is not possible to add fields during the translation process. (Missing fields will be ignored.)

This is because the reference for an entity is the unique ID of the Django CMS plugin, and each plugin has its own unique ID for each page and language.

Therefore, you need to follow these steps to work with djangocms-xliff.

Step-by-step

If the page does not exist yet in the target language, create it and copy the plugins from the page with the source plugins.

Go to the page in the target language.

Export the XLIFF file at Language > Export as XLIFF…

Export

This will generate an XLIFF file in the following format:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
    <file original="verbund/meilen/projekt" datatype="plaintext" source-language="fr" target-language="en">
        <tool tool-id="96" tool-name="djangocms_xliff" tool-company-name="Energie 360°"/>
        <body>
            <trans-unit id="5872__title" resname="5872__title" maxwidth="60" size-unit="char"
                        extype="django.db.models.fields.CharField">
                <source><![CDATA[The project in short]]></source>
                <target><![CDATA[]]></target>
                <note>CarouselTripleBlockWrapperPlugin</note>
                <note>Carousel Triple Block</note>
                <note>Titel</note>
                <note>Max characters: 60</note>
            </trans-unit>
            <trans-unit id="5874__title" resname="5874__title" maxwidth="35" size-unit="char"
                        extype="django.db.models.fields.CharField">
                <source><![CDATA[Practical Solution]]></source>
                <target><![CDATA[]]></target>
                <note>CarouselTripleBlockSlidePlugin</note>
                <note>Slide</note>
                <note>Titel</note>
                <note>Max characters: 35</note>
            </trans-unit>
        </body>
    </file>
</xliff>

Edit the file in the XLIFF editor of your choice.

Import the XLIFF to the same page in the same language you exported from with Languages > Import from XLIFF

Import

You will get a preview of the import that needs to be confirmed.

Preview

The translations are now imported, and you can publish the page.

Settings

By default, djangocms-xliff searches for the following django model fields: CharField, SlugField, TextField, URLField in your plugins. The texts from these fields will be used for the XLIFF import and export.

If you want to add additional or 3rd party app fields, you can define the following settings in your settings.py, to integrate them into the XLIFF package:

# A list of fields, that will be searched for while exporting.
DJANGOCMS_XLIFF_FIELDS = (
    "djangocms_text_ckeditor.fields.HTMLField",
)
# List of tuples with field and custom function for the export
DJANGOCMS_XLIFF_FIELD_EXTRACTORS = (
    ("third_party.models.LinkField", "your_module.xliff.link_field_extractor"),
)


# The signature of the extractor function must be the following:
# The source parameter is the same as getattr(instance, field.name)
def link_field_extractor(instance: CMSPlugin, field: LinkField, source: Any) -> List[djangocms_xliff.types.Unit]:
    # example:
    from djangocms_xliff.utils import get_type_with_path

    text = source.find_text()
    return [
        Unit(
            plugin_id=str(instance.pk),
            plugin_type=instance.plugin_type,
            plugin_name=instance.get_plugin_name(),
            field_name=field.name,
            field_type=get_type_with_path(field),
            field_verbose_name=field.verbose_name,
            source=text,
            max_length=field.max_length,
        )
    ]
# List of tuples with field and custom function for the import
DJANGOCMS_XLIFF_FIELD_IMPORTERS = (
    ("third_party.models.LinkField", "your_module.xliff.link_field_importer"),
)


# The signature of the importer function must be the following:
def link_field_importer(instance: CMSPlugin, unit: djangocms_xliff.types.Unit) -> CMSPlugin:
    # example:
    field = getattr(instance, unit.field_name)
    field.body = unit.target
    return instance
# List of custom validators for fields that need to be ignored or included in the export
DJANGOCMS_XLIFF_VALIDATORS = ("your_module.xliff.is_not_background",)


# The signature of the validator function must be the following:
def is_not_background(field: django.db.models.Field, instance: CMSPlugin) -> bool:
    # example:
    return field.name != "background"

Placeholders Outside the CMS

This package does not handle translatability at database level. There are various packages for that. We recommend the use of django-modeltranslation. Because this way import and export of XLIFF works out-of-the-box.

You can import / export any django model with this package. In your admin.py add the following code:

from django.contrib import admin
from djangocms_xliff.admin import XliffImportExportMixin

from magazine.models import Article


@admin.register(Article)
class ArticleAdmin(XliffImportExportMixin, admin.ModelAdmin):
    pass

You can customize the fields that are exported with the following configuration:

# By default djangocms_xliff.settings.METADATA_FIELDS fields get exported. You can include and exclude fields like this:
DJANGOCMS_XLIFF_MODEL_METADATA_FIELDS = {
    'magazine.models.Article': {
        "include": {
            "lead": _("Lead"),
        },
        'exclude': ["slug", "og_title", "og_description"]
    }
}

If you have a custom alias content in your app, you need to set the following setting variable:

DJANGOCMS_XLIFF_MODEL_FOR_ALIAS_CONTENT = "your_module.xliff.get_model_for_alias_content"


# The signature of the path function must be the following:
def get_model_for_alias_content(alias):
    if hasattr(alias, "magazine_article"):
        return alias.magazine_article
    return None

You need to make sure that the your model has a get_absolute_url(language: str) method for everything to work.

Contribute

Issues and pull requests are welcomed.

You can find a documentation on how to set up your project in here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangocms_xliff-2.0.4.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangocms_xliff-2.0.4-py3-none-any.whl (40.5 kB view details)

Uploaded Python 3

File details

Details for the file djangocms_xliff-2.0.4.tar.gz.

File metadata

  • Download URL: djangocms_xliff-2.0.4.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.2

File hashes

Hashes for djangocms_xliff-2.0.4.tar.gz
Algorithm Hash digest
SHA256 f2aacfa23b973a42600da2f2d9fef1745e4a1c8327f641e7f777a0f7168535f9
MD5 5b1e898653659d08efe05195156285e3
BLAKE2b-256 0448b548418ffe3ce641530565e7132f6ea622c912b5cf2334cbdf73dc479640

See more details on using hashes here.

File details

Details for the file djangocms_xliff-2.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for djangocms_xliff-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dda783c0bc7e057986770e2cddb6bc8627589a60b562582dba761fe1907731cc
MD5 41806f197e6849ae222fafcc7a1435d8
BLAKE2b-256 a6b131c8c17ebc723b2aa79a9236e786fa4da00ecf54cb2dc809ab0cd65c6537

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page