XLIFF
This package contains helper classes for the handling of XLIFF files.
It can handle XLIFF files of version 1.2 (see xliff1_serializer
) and 2.0 (see
xliff2_serializer
).
Additionally, it contains a base_serializer
containing shared functionality of both XLIFF versions and a
generic_serializer
, which inspects the given XLIFF file and delegates its deserialization to the
respective version’s deserializer.
The interface follows Django’s standard serialization syntax (see Serializing Django objects):
serializers.serialize("xliff", queryset)
serializers.deserialize("xliff", xliff_file)
To facilitate the handling of XLIFF files in the views, this package also contains utilities in utils
.
Apps
Configuration of XLIFF app
Base Serializer
This module contains the abstract base classes for the XLIFF serializers. It makes use of the existing Django serialization functionality (see Serializing Django objects and Serialization formats).
It extends django/core/serializers/base.py and django/core/serializers/xml_serializer.py.
- class integreat_cms.xliff.base_serializer.Deserializer(stream_or_string, *, using='default', ignorenonexistent=False, **options)[source]
Bases:
Deserializer
Abstract base XLIFF deserializer class. Inherits basic XML initialization from the default xml_serializer of Django. The contents of the XLIFF file are available through
self.event_stream
, which gets assigned to the result ofxml.dom.pulldom.parse()
.- static get_language(attribute: str) Language [source]
Get the language object to a given
bcp47_tag
orslug
.- Parameters:
attribute (str) – The
bcp47_tag
orslug
of the requested language- Raises:
DoesNotExist – If no language exists with the given attribute
- Returns:
The requested language
- Return type:
- get_object(node: Element) PageTranslation [source]
Retrieve an object from the serialized unit node. To be implemented in the subclass of this base serializer.
- Parameters:
node (Element) – The current xml node of the object
- Raises:
NotImplementedError – If the property is not implemented in the subclass
- Return type:
- handle_object(node: Element) DeserializedObject [source]
Convert a
<file>
-node to aDeserializedObject
.- Parameters:
node (Element) – The current xml node of the object
- Raises:
DeserializationError – If the deserialization fails
FieldDoesNotExist – If the XLIFF file contains a field which doesn’t exist on the
- Returns:
The deserialized page translation PageTranslation model
- Return type:
DeserializedObject
- class integreat_cms.xliff.base_serializer.Serializer[source]
Bases:
Serializer
Abstract base XLIFF serializer class. Inherits basic XML initialization from the default xml_serializer of Django (see Serialization formats).
The XLIFF file can be extended by writing to
self.xml
, which is an instance ofXMLGeneratorWithCDATA
.For details, look at the implementation of django/core/serializers/base.py and django/core/serializers/xml_serializer.py.
- end_object(obj: PageTranslation) None [source]
Called when serializing of an object ends.
- Parameters:
obj (PageTranslation) – The page translation object which is finished
- Return type:
None
- end_serialization() None [source]
End serialization by ending the
<xliff>
-block and the document.- Return type:
None
- getvalue() str | None [source]
Return the fully serialized translation (or
None
if the output stream is not seekable).- Returns:
The output XLIFF string
- Return type:
str | None
- handle_field(obj: PageTranslation, field: CharField | TextField) None [source]
Called to handle each field on an object (except for ForeignKeys and ManyToManyFields)
- Parameters:
obj (PageTranslation) – The page translation object which is handled
field (CharField | TextField) – The model field
- Raises:
NotImplementedError – If the property is not implemented in the subclass
- Return type:
None
- handle_fk_field(obj: PageTranslation, field: ForeignKey) None [source]
ForeignKey fields are not supported by this serializer. They will just be ignored and are not contained in the resulting XLIFF file.
- Parameters:
obj (PageTranslation) – The page translation object which is handled
field (ForeignKey) – The foreign key field
- Return type:
None
- handle_m2m_field(obj: PageTranslation, field: ManyToManyField) None [source]
ManyToMany fields are not supported by this serializer. They will just be ignored and are not contained in the resulting XLIFF file.
- Parameters:
obj (PageTranslation) – The page translation object which is handled
field (ManyToManyField) – The many to many field
- Return type:
None
- serialize(queryset: list[PageTranslation], *args: Any, **kwargs: Any) str [source]
Initialize serialization and set the
XLIFF_DEFAULT_FIELDS
.- Parameters:
queryset (list[PageTranslation]) – QuerySet of all
PageTranslation
objects which should be serialized*args (Any) – The remaining arguments
**kwargs (Any) – The supplied keyword arguments
- Returns:
The serialized XLIFF string
- Return type:
- start_object(obj: PageTranslation) None [source]
Called when serializing of an object starts.
- Parameters:
obj (PageTranslation) – The page translation object which is started
- Raises:
NotImplementedError – If the property is not implemented in the subclass
- Return type:
None
- start_serialization() None [source]
Start serialization - open the XML document and the root element.
- Return type:
None
- xml: XMLGeneratorWithCDATA | None = None[source]
The XML generator of this serializer instance
- class integreat_cms.xliff.base_serializer.XMLGeneratorWithCDATA(out=None, encoding='iso-8859-1', short_empty_elements=False)[source]
Bases:
SimplerXMLGenerator
Subclass of SimplerXMLGenerator to provide a custom CDATA node
Generic Serializer
- class integreat_cms.xliff.generic_serializer.Deserializer(*args: str, **kwargs: Any)[source]
Bases:
Deserializer
For deserialization, inspect the data and choose the correct version
- Parameters:
args (str)
kwargs (Any)
- __init__(*args: str, **kwargs: Any) None [source]
Init this serializer given a stream or a string
- Parameters:
args (str)
kwargs (Any)
- Return type:
None
- get_object(node: Element) PageTranslation [source]
Retrieve an object from the serialized unit node. Handled by either
get_object()
orget_object()
- Parameters:
node (Element) – The current xml node of the object
- Returns:
The original page translation
- Return type:
- handle_object(node: Element) DeserializedObject [source]
Convert a
<file>
-node to a DeserializedObject. Handled by eitherhandle_object()
orhandle_object()
- Parameters:
node (Element) – The current xml node of the object
- Returns:
The deserialized page translation
- Return type:
DeserializedObject
- class integreat_cms.xliff.generic_serializer.Serializer[source]
Bases:
Serializer
For serialization, just fall back to the serializer for XLIFF version 2.0
Utils
Helper functions for the XLIFF serializer
- integreat_cms.xliff.utils.get_translation_key(translation: PageTranslation) str [source]
Return a unique key for each translation to be able to find duplicates. We cannot use the translation id here because the objects have not been written to the database.
- Parameters:
translation (PageTranslation) – The translation
- Returns:
A unique key for this
Page
/Language
/version
combination- Return type:
- integreat_cms.xliff.utils.get_xliff_import_diff(request: HttpRequest, xliff_dir: str) list[dict[str, Any]] [source]
Show the XLIFF import diff
- integreat_cms.xliff.utils.get_xliff_import_errors_and_clean_translation(request: HttpRequest, page_translation: PageTranslation, add_message_if_unchanged: bool = False) tuple[list, bool] [source]
Validate an imported page translation and return all errors As a side effect, a unique slug is generated for the translation if it does not yet have one. Additionally, the content of the translation will be cleaned by a PageTranslationForm.
- Parameters:
request (HttpRequest) – The current request (used for error messages)
page_translation (PageTranslation) – The page translation which is being imported
add_message_if_unchanged (bool) – Whether a message should be added if no changes are detected
- Returns:
All errors of this XLIFF import
- Return type:
- integreat_cms.xliff.utils.page_to_xliff(page: Page, target_language: Language, dir_name: UUID, only_public: bool = False) str [source]
Export a page to an XLIFF file for a specified target language
- Parameters:
- Raises:
RuntimeWarning – If the selected page translation does not have a source translation
RuntimeError – When an unexpected error occurs during serialization
- Returns:
The path of the generated XLIFF file
- Return type:
- integreat_cms.xliff.utils.pages_to_xliff_file(request: HttpRequest, pages: PageQuerySet, target_languages: list[Language], only_public: bool = False) str | None [source]
Export a list of page IDs to a ZIP archive containing XLIFF files for a specified target language (or just a single XLIFF file if only one page is converted)
- Parameters:
request (HttpRequest) – The current request (used for error messages)
pages (PageQuerySet) – list of pages which should be translated
target_languages (list[Language]) – list of target languages (should exclude the region’s default language)
only_public (bool) – Whether only public versions should be exported
- Returns:
The path of the generated zip file
- Return type:
str | None
- integreat_cms.xliff.utils.xliff_import_confirm(request: HttpRequest, xliff_dir: str, machine_translated: bool) bool [source]
Confirm the XLIFF import and write changes to database
XLIFF 1.2 Serializer
- class integreat_cms.xliff.xliff1_serializer.Deserializer(stream_or_string, *, using='default', ignorenonexistent=False, **options)[source]
Bases:
Deserializer
XLIFF deserializer class for XLIFF version 1.2
- get_object(node: Element) PageTranslation [source]
Retrieve an object from the serialized unit node.
- Parameters:
node (Element) – The current xml node of the object
- Raises:
DeserializationError – If the deserialization fails
- Returns:
The original page translation
- Return type:
- class integreat_cms.xliff.xliff1_serializer.Serializer[source]
Bases:
Serializer
XLIFF serializer class for XLIFF version 1.2. This was inspired by django-xliff.
- end_object(obj: PageTranslation) None [source]
Called after handling all fields for an object. Ends the
<file>
-block.- Parameters:
obj (PageTranslation) – The page translation object which is finished
- Return type:
None
- handle_field(obj: PageTranslation, field: CharField | TextField) None [source]
Called to handle each field on an object (except for ForeignKeys and ManyToManyFields)
- Parameters:
obj (PageTranslation) – The page translation object which is handled
field (CharField | TextField) – The model field
- Return type:
None
- start_object(obj: PageTranslation) None [source]
Called as each object is handled. Adds an XLIFF
<file>
-block with meta-information about the object and an additional<body>
for XLIFF version 1.2.- Parameters:
obj (PageTranslation) – The page translation object which is started
- Raises:
SerializationError – If the serialization fails
- Return type:
None
XLIFF 2.0 Serializer
- class integreat_cms.xliff.xliff2_serializer.Deserializer(*args: Any, **kwargs: Any)[source]
Bases:
Deserializer
XLIFF deserializer class for XLIFF version 2.0
Deserializes XLIFF files into
PageTranslation
objects.- Parameters:
args (Any)
kwargs (Any)
- __init__(*args: Any, **kwargs: Any) None [source]
Initialize XLIFF 2.0 deserializer
- Parameters:
*args (Any) – The supplied arguments
**kwargs (Any) – The supplied keyword arguments
- Raises:
DeserializationError – If the deserialization fails
- Return type:
None
- get_object(node: Element) PageTranslation [source]
Retrieve an object from the serialized unit node. To be implemented in the subclass of this base serializer.
- Parameters:
node (Element) – The current xml node of the object
- Returns:
The original page translation
- Return type:
- class integreat_cms.xliff.xliff2_serializer.Serializer[source]
Bases:
Serializer
XLIFF serializer class for XLIFF version 2.0
Serializes
PageTranslation
objects into translatable XLIFF files.- end_object(obj: PageTranslation) None [source]
Called after handling all fields for an object. Ends the
<file>
-block.- Parameters:
obj (PageTranslation) – The page translation object which is finished
- Return type:
None
- handle_field(obj: PageTranslation, field: CharField | TextField) None [source]
Called to handle each field on an object (except for ForeignKeys and ManyToManyFields)
- Parameters:
obj (PageTranslation) – The page translation object which is handled
field (CharField | TextField) – The model field
- Raises:
SerializationError – If the serialization fails
- Return type:
None
- serialize(queryset: list[PageTranslation], *args: Any, **kwargs: Any) str [source]
Initialize serialization and find out in which source and target language the given elements are.
- Parameters:
queryset (list[PageTranslation]) – QuerySet of all
PageTranslation
objects which should be serialized*args (Any) – The remaining arguments
**kwargs (Any) – The supplied keyword arguments
- Raises:
SerializationError – If the serialization fails
- Returns:
The serialized XLIFF string
- Return type:
- source_language = None[source]
The source language of this serializer instance
- start_object(obj: PageTranslation) None [source]
Called as each object is handled. Adds an XLIFF
<file>
-block with meta-information about the object.- Parameters:
obj (PageTranslation) – The page translation object which is started
- Return type:
None
- start_serialization() None [source]
Start serialization - open the XML document and the root element.
- Return type:
None
- target_language = None[source]
The target language of this serializer instance