This commit is contained in:
2024-09-02 18:44:31 +02:00
parent 8516703bc4
commit 00b4dd68f6
11 changed files with 108 additions and 51 deletions

View File

@ -5,7 +5,7 @@ cd "${swd}/.."
export sitocttEnv=Offline
export HUGO_ENV=production
sh "${swd}/Clean.sh"
sh "${swd}/Update.sh"
sh "${swd}/Build.sh"
cd ./build/public
#zip -9 -r ./offline.zip *

View File

@ -4,7 +4,7 @@ cd "${swd}/.."
export HUGO_ENV=production
sh "${swd}/Clean.sh"
sh "${swd}/Update.sh"
sh "${swd}/Build.sh"
sh "${swd}/Redirects.sh"
cd "${owd}"

View File

@ -61,4 +61,7 @@ cd ./Scripts
PIP_BREAK_SYSTEM_PACKAGES=1 "${Python3}" -m pip install -U -r ./requirements.txt
#npm install
cd ./Lib
wget -O ./translate-shell.bash https://git.io/trans
cd "${owd}"

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
DestinationLanguages = ["it", "en"] # "fr", "de", "eo", "es"
DestinationLanguages = ["it", "en", "fr"] # "de", "eo", "es"
IncludePaths = ["/"]
ExcludePaths = ["/Categories"]
ExcludePaths = ["/categories"] # "/miscellanea"
import subprocess
from os import getcwd, listdir
from os.path import dirname, realpath, isfile, isdir
from pathlib import Path
@ -53,32 +54,63 @@ def find_documents(folder_path):
documents[document].append(destination_language)
return documents
def fix_frontmatter(translated_text, reference_text):
result = ''
reference_lines = reference_text.splitlines()
for [index, translated_line] in enumerate(translated_text.splitlines()):
if translated_line.strip() and (translated_line.lstrip() == translated_line):
reference_line = reference_lines[index]
line_key = reference_line.split('=')[0]
if line_key.strip().lower() in ["draft", "date", "lastmod"]:
translated_line = reference_line
else:
line_value = '='.join(translated_line.split('=')[1:])
translated_line = line_key
if line_value:
translated_line += ('=' + line_value)
result += (translated_line + '\n')
return result
def translate_document(document_path, documents):
printf(f'* {document_path} ->')
printf(f"* {document_path} ->")
for destination_language in documents[document_path]:
source_language = get_source_language(document_path)
original_text = open(("../content/" + document_path), 'r').read()
printf('', destination_language)
try:
translated = translate(
open(('../content/' + document_path), 'r').read(),
destination_language,
source_language)
is_python_translator = True
translated = translate(original_text, destination_language, source_language)
if not len(translated.results):
raise Exception("Unhandled error")
printf('')
except Exception as exception:
printf('', exception)
continue
translated_text = translated.results[0].paraphrase
try:
is_python_translator = False
translated = subprocess.run(
("bash", "../Scripts/Lib/translate-shell.bash", "-brief",
"-t", destination_language, "-s", source_language,
("file://" + "../content/" + document_path)),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if translated.stderr:
raise Exception(translated.stderr.decode())
except Exception as exception:
printf('', exception)
continue
printf('')
translated_text = (translated.results[0].paraphrase
if is_python_translator else translated.stdout.decode())
text_header = translated_text.strip().splitlines()[0].strip()
translated_preamble = ("\n\n{{< noticeAutomaticTranslation " + source_language + " >}}\n\n")
if text_header in ["---", "+++"]:
text_tokens = translated_text.split(text_header)
translated_text = (
text_header + text_tokens[1] + text_header +
translated_preamble +
text_header.join(text_tokens[2:]))
else:
translated_body = text_header.join(text_tokens[2:])
translated_text = (text_header +
fix_frontmatter(text_tokens[1], original_text.split(text_header)[1]) +
text_header)
if translated_body.strip():
translated_text += (translated_preamble + translated_body)
elif translated_text.strip():
translated_text = (translated_preamble + translated_text)
destination_path = make_destination_path(document_path, destination_language)
Path('/'.join(destination_path.split('/')[:-1])).mkdir(parents=True, exist_ok=True)