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
- class integreat_cms.xliff.apps.XLIFFConfig(app_name, app_module)[source]
Bases:
AppConfig
XLIFF config inheriting the django AppConfig
- classmethod create(entry)[source]
Factory that creates an app config from an entry in INSTALLED_APPS.
- get_model(model_name, require_ready=True)[source]
Return the model with the given case-insensitive model_name.
Raise LookupError if no model exists with this name.
- get_models(include_auto_created=False, include_swapped=False)[source]
Return an iterable of models.
By default, the following models aren’t included:
auto-created models for many-to-many relations without an explicit intermediate table,
models that have been swapped out.
Set the corresponding keyword argument to True to include such models. Keyword arguments aren’t documented; they’re a private API.
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()
.- __init__(stream_or_string, *, using='default', ignorenonexistent=False, **options)[source]
Init this serializer given a stream or a string
- 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
- addQuickElement(name, contents=None, attrs=None)[source]
Convenience method for adding an element with no children
- cdata(content: str) None [source]
Create a
<![CDATA[]>
-block with the given content- Parameters:
content (str) – The given
CDATA
content- Return type:
None
- characters(content)[source]
Receive notification of character data.
The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.
- endDocument()[source]
Receive notification of the end of a document.
The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input.
- endElement(name)[source]
Signals the end of an element in non-namespace mode.
The name parameter contains the name of the element type, just as with the startElement event.
- endElementNS(name, qname)[source]
Signals the end of an element in namespace mode.
The name parameter contains the name of the element type, just as with the startElementNS event.
- endPrefixMapping(prefix)[source]
End the scope of a prefix-URI mapping.
See startPrefixMapping for details. This event will always occur after the corresponding endElement event, but the order of endPrefixMapping events is not otherwise guaranteed.
- ignorableWhitespace(content)[source]
Receive notification of ignorable whitespace in element content.
Validating Parsers must use this method to report each chunk of ignorable whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.
SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.
- processingInstruction(target, data)[source]
Receive notification of a processing instruction.
The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.
A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.
- setDocumentLocator(locator)[source]
Called by the parser to give the application a locator for locating the origin of document events.
SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.
The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application’s business rules). The information returned by the locator is probably not sufficient for use with a search engine.
Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.
- skippedEntity(name)[source]
Receive notification of a skipped entity.
The Parser will invoke this method once for each entity skipped. Non-validating processors may skip entities if they have not seen the declarations (because, for example, the entity was declared in an external DTD subset). All processors may skip external entities, depending on the values of the http://xml.org/sax/features/external-general-entities and the http://xml.org/sax/features/external-parameter-entities properties.
- startDocument()[source]
Receive notification of the beginning of a document.
The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator).
- startElement(name, attrs)[source]
Signals the start of an element in non-namespace mode.
The name parameter contains the raw XML 1.0 name of the element type as a string and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.
- startElementNS(name, qname, attrs)[source]
Signals the start of an element in namespace mode.
The name parameter contains the name of the element type as a (uri, localname) tuple, the qname parameter the raw XML 1.0 name used in the source document, and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.
The uri part of the name tuple is None for elements which have no namespace.
- startPrefixMapping(prefix, uri)[source]
Begin the scope of a prefix-URI Namespace mapping.
The information from this event is not necessary for normal Namespace processing: the SAX XML reader will automatically replace prefixes for element and attribute names when the http://xml.org/sax/features/namespaces feature is true (the default).
There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary.
Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each-other: all startPrefixMapping events will occur before the corresponding startElement event, and all endPrefixMapping events will occur after the corresponding endElement event, but their order is not guaranteed.
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
- 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. 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
- 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
- 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:
SerializationError – If the serialization fails
- 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 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:
- 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
- xml: XMLGeneratorWithCDATA | None = None[source]
The XML generator of this serializer instance
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
- __init__(stream_or_string, *, using='default', ignorenonexistent=False, **options)[source]
Init this serializer given a stream or a string
- 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.
- Parameters:
node (Element) – The current xml node of the object
- Raises:
DeserializationError – If the deserialization fails
- Returns:
The original page translation
- 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.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
- 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
- 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 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
- 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
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
- 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
- Returns:
The original page translation
- 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.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
- 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:
SerializationError – If the serialization fails
- 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 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:
- 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
- xml: XMLGeneratorWithCDATA | None = None[source]
The XML generator of this serializer instance