translation working but each translation gets inside each parent

This commit is contained in:
Guilhem Fauré 2023-06-02 15:56:12 +02:00
parent 06230584d8
commit c10c6d81fe

View File

@ -1,6 +1,5 @@
# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré # SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré
import logging import logging
from copy import deepcopy
from os import makedirs from os import makedirs
from os.path import basename, splitext from os.path import basename, splitext
from re import Pattern, finditer, search from re import Pattern, finditer, search
@ -316,7 +315,7 @@ class RedactionalObject(WritableObject):
# Detect every language present in <multi> blocks of text # Detect every language present in <multi> blocks of text
# For each language in <multi> block in which we want to translate, create # For each language in <multi> block in which we want to translate, create
# a new self-similar object in self.translations dict # a new self-similar object in self.translations dict
def translate_multi(self, spipattr: str, convertattr: str) -> str: def translate_multi(self, spipattr: str) -> str:
# Function specific logger # Function specific logger
log = logging.getLogger(CFG.logname + ".models.translate_multi") log = logging.getLogger(CFG.logname + ".models.translate_multi")
text: str = getattr(self, spipattr) # Get text of attribute text: str = getattr(self, spipattr) # Get text of attribute
@ -354,10 +353,10 @@ class RedactionalObject(WritableObject):
if lang in CFG.export_languages: if lang in CFG.export_languages:
if lang not in self._translations: if lang not in self._translations:
self._translations[lang] = {} self._translations[lang] = {}
self._translations[lang][convertattr] = translation self._translations[lang][spipattr] = translation
log.debug( log.debug(
f"{lang} `{self._title}` `{convertattr}`" f"{lang} `{self._title}` `{spipattr}`"
+ f" set to `{self._translations[lang][convertattr]}`" + f" set to `{self._translations[lang][spipattr]}`"
) )
return original_translation return original_translation
@ -407,7 +406,7 @@ class RedactionalObject(WritableObject):
LOG.debug(f"{type(self).__name__} title is empty") LOG.debug(f"{type(self).__name__} title is empty")
return "" return ""
self._title = self.titre.strip() # Define temporary title to use in functions self._title = self.titre.strip() # Define temporary title to use in functions
self._title = self.translate_multi("titre", "_title") self._title = self.translate_multi("titre")
LOG.debug( LOG.debug(
f"`{self.lang}` `{self._title}` title was translated to : `{self._title}`" f"`{self.lang}` `{self._title}` title was translated to : `{self._title}`"
) )
@ -430,7 +429,7 @@ class RedactionalObject(WritableObject):
if len(self.texte) == 0: if len(self.texte) == 0:
LOG.debug(f"{type(self).__name__} {self._title} text is empty") LOG.debug(f"{type(self).__name__} {self._title} text is empty")
return "" return ""
self._text = self.translate_multi("texte", "_title") self._text = self.translate_multi("texte")
LOG.debug(f"Convert document links of `{self._title}`") LOG.debug(f"Convert document links of `{self._title}`")
self._text = self.replace_links(self._text, DOCUMENT_LINK, Document) self._text = self.replace_links(self._text, DOCUMENT_LINK, Document)
LOG.debug(f"Convert article links of `{self._title}`") LOG.debug(f"Convert article links of `{self._title}`")
@ -533,14 +532,30 @@ class RedactionalObject(WritableObject):
translations: list[Self] = [] translations: list[Self] = []
LOG.debug(f"`{self._title}` contains translations: `{self._translations}`") LOG.debug(f"`{self._title}` contains translations: `{self._translations}`")
for lang, translated_attrs in self._translations.items(): for lang, translated_attrs in self._translations.items():
LOG.debug(f"Instanciating {lang} translation of section `{self._title}`")
# Copy itself (with every attribute) as a base for the translated object # Copy itself (with every attribute) as a base for the translated object
# translation: Self = deepcopy(self) # translation: Self = deepcopy(self)
# Define attributes that new translation will need # Define attributes that new translation will need
attributes: dict[str, str] = {} attributes: dict[str, Any] = {
translation: Self = type(self)(**translated_attrs, **attributes) "id_trad": self.id_trad,
# Replace the lang & the translations attributes of the translated object "titre": self.titre,
"date": self.date,
"maj": self.maj,
"statut": self.statut,
"descriptif": self.descriptif,
"id_secteur": self.id_secteur,
}
# Replace default attributes with translated ones
for attr, value in translated_attrs.items():
attributes[attr] = value
LOG.debug(
f"Instanciating {lang} translation of section `{self._title}`"
+ f" with attributes {attributes}"
)
translation: Self = type(self)(**attributes)
# Replace important attributes of the translated object
translation.lang = lang translation.lang = lang
translation._id = self._id # WARNING ?
translation._depth = self._depth # WARNING ?
translation._translations = {} translation._translations = {}
# Replace the translated attributes of the translated object # Replace the translated attributes of the translated object
# For each translated attribute, output a DEBUG message # For each translated attribute, output a DEBUG message