mirror of
https://gitlab.com/octtspacc/sitoctt
synced 2025-06-05 22:09:20 +02:00
Salv. script traduzione
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from pathlib import Path
|
||||
from deepl import deepl
|
||||
|
||||
@ -14,20 +15,34 @@ def IgnoreFiles(Dir, Files):
|
||||
|
||||
def FindTextFiles():
|
||||
List = []
|
||||
for Dir in ('Pages/', 'Posts/'):
|
||||
for Ext in ('htm', 'html', 'markdown', 'md', 'pug', 'txt'):
|
||||
for File in Path(Dir).rglob(f'*.{Ext}'):
|
||||
List += [File]
|
||||
#for Dir in ('Posts/'): #('Pages/', 'Posts/'):
|
||||
for Ext in ('htm', 'html', 'markdown', 'md', 'pug', 'txt'):
|
||||
for File in Path(Dir).rglob(f'*.{Ext}'):
|
||||
List += [File]
|
||||
return List
|
||||
|
||||
def GetMetaComment(Paragraph, Num, Count):
|
||||
return f'<!-- Paragraph {abs(Count-Num)} {hashlib.md5(Paragraph.encode()).hexdigest()} {{TranslationHash}} --->'
|
||||
def GetMetaComment(Paragraph, Num, Count, Translation=None):
|
||||
return f'<!-- Paragraph {abs(Count-Num)} {hashlib.md5(Paragraph.encode()).hexdigest()} {hashlib.md5(Translation.encode()).hexdigest() if Translation else ""} --->'
|
||||
|
||||
def StrReverse(Str):
|
||||
_ = list(Str)
|
||||
_.reverse()
|
||||
return ''.join(_)
|
||||
|
||||
def TryTranslate(Text):
|
||||
try:
|
||||
#Trans += GetMetaComment(Target, i) + '\n' + Translate.translate(Target) + '\n\n'
|
||||
#Trans = GetMetaComment(Target, i, len(Paragraphs)) + '\n' + Translate.translate(Target) + '\n\n' + Trans
|
||||
return Translate.translate(Text)
|
||||
#Target = ''
|
||||
#except deepl.DeepLCLIPageLoadError:
|
||||
# raise
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
#Trans += GetMetaComment(Target, i) + '\n' + Target + '\n\n'
|
||||
#Trans = GetMetaComment(Target, i, len(Paragraphs)) + '\n' + Target + '\n\n' + Trans
|
||||
|
||||
DestLangs.remove(SourceLang)
|
||||
for Lang in DestLangs:
|
||||
Translate = deepl.DeepLCLI(SourceLang, Lang)
|
||||
@ -35,6 +50,9 @@ for Lang in DestLangs:
|
||||
shutil.copytree(Dir, f'i18n/{Lang}/{Dir}', ignore=IgnoreFiles, dirs_exist_ok=True)
|
||||
|
||||
for File in FindTextFiles():
|
||||
if os.path.isfile(f'i18n/{Lang}/{File}'): # Skip already translated pages - This will evolve into a skip only if not updated
|
||||
continue
|
||||
|
||||
with open(File, 'r') as f:
|
||||
FullSource, Target, Trans = f.read(), '', ''
|
||||
print(f'\n{Lang}/{File}:', end='')
|
||||
@ -42,6 +60,7 @@ for Lang in DestLangs:
|
||||
Paragraphs = StrReverse(FullSource).split('\n\n')
|
||||
for i,Paragraph in enumerate(Paragraphs): # Assuming no single paragraph is > 5000 chars
|
||||
print(f' {i}', end='')
|
||||
Backoff = 0
|
||||
Target = StrReverse(Paragraph)
|
||||
#if len(Target+Paragraph) < 5000:
|
||||
# Target += GetMetaComment(Paragraph, Num) + '\n' + Paragraph + '\n\n'
|
||||
@ -52,19 +71,18 @@ for Lang in DestLangs:
|
||||
if not Target: # There were more than 2 line breaks
|
||||
Trans += '\n\n'
|
||||
continue
|
||||
Done = ''
|
||||
while not Done:
|
||||
time.sleep(5 + (Backoff*1.25))
|
||||
Backoff += 1.25
|
||||
Done = TryTranslate(Target)
|
||||
Trans = GetMetaComment(Target, i, len(Paragraphs), Done) + '\n' + Done + '\n\n' + Trans
|
||||
#while True:
|
||||
try:
|
||||
#Trans += GetMetaComment(Target, i) + '\n' + Translate.translate(Target) + '\n\n'
|
||||
Trans = GetMetaComment(Target, i, len(Paragraphs)) + '\n' + Translate.translate(Target) + '\n\n' + Trans
|
||||
Target = ''
|
||||
except deepl.DeepLCLIPageLoadError:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(e)
|
||||
#Trans += GetMetaComment(Target, i) + '\n' + Target + '\n\n'
|
||||
Trans = GetMetaComment(Target, i, len(Paragraphs)) + '\n' + Target + '\n\n' + Trans
|
||||
#continue
|
||||
#continue
|
||||
#Translate.translate(Paragraph)
|
||||
#Trans += os.popen(f'cat ./Translate.tmp | deepl -s --fr {SourceLang} --to {Lang}').read()
|
||||
print(Trans)
|
||||
exit()
|
||||
|
||||
with open(f'i18n/{Lang}/{File}', 'w') as f:
|
||||
f.write(Trans)
|
||||
#print(Trans)
|
||||
#exit()
|
||||
|
Reference in New Issue
Block a user