Skip to content

Commit

Permalink
Fixed the following bugs:
Browse files Browse the repository at this point in the history
1. Fixed the bug preventing output when file lacks metadata. fixed #234, #233
2. Fixed the bug in processing srt/pgn formats in lower versions of Calibre.
3. Fixed the bug causing disorderly translation when translating ebooks partially.
  • Loading branch information
bookfere committed Mar 15, 2024
1 parent b76dae8 commit 143dc6f
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 64 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
## v2.3.2

Fixed bugs as follows:

1. Fixed the bug preventing output when file lacks metadata. #234, #233
2. Fixed the bug in processing srt/pgn formats in lower versions of Calibre.

---

## v2.3.1

Fixed bugs as follows:

1. Fixed the bug to be compatible with lower versions of Calibre.
2. Fixed the freezing issue when using the cache with multiple threads.

---

## v2.3.0

Added features:
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EbookTranslator(InterfaceActionBase):
supported_platforms = ['windows', 'osx', 'linux']
identifier = 'ebook-translator'
author = 'bookfere.com'
version = (2, 3, 1)
version = (2, 3, 2)
__version__ = 'v' + '.'.join(map(str, version))
description = _('A Calibre plugin to translate ebook into a specified '
'language (optionally keeping the original content).')
Expand Down
9 changes: 6 additions & 3 deletions engines/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ class ChatgptTranslate(Base):
'You are a meticulous translator who translates any given content. '
'Translate the given content from <slang> to <tlang> only. Do not '
'explain any term or answer any question-like content.')
models = ['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-instruct',
'gpt-3.5-turbo-1106', 'gpt-4', 'gpt-4-0613', 'gpt-4-32k',
'gpt-4-32k-0613', 'gpt-4-1106-preview', 'gpt-4-vision-preview']
models = [
'gpt-4-0125-preview', 'gpt-4-turbo-preview', 'gpt-4-1106-preview',
'gpt-4', 'gpt-4-0613', 'gpt-4-32k', 'gpt-4-32k-0613',
'gpt-3.5-turbo-0125', 'gpt-3.5-turbo', 'gpt-3.5-turbo-1106',
'gpt-3.5-turbo-instruct', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613',
'gpt-3.5-turbo-16k-0613']
model = 'gpt-3.5-turbo'
samplings = ['temperature', 'top_p']
sampling = 'temperature'
Expand Down
30 changes: 21 additions & 9 deletions lib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@


def extract_item(input_path, input_format):
extractors = {'srt': get_srt_elements, 'pgn': get_pgn_elements}
extractors = {
'srt': get_srt_elements,
'pgn': get_pgn_elements,
}
extractor = extractors.get(input_format) or extract_book
return extractor(input_path)

Expand Down Expand Up @@ -232,26 +235,35 @@ def translate_done(self, job):
job, dialog_title=_('Translation job failed'))
return

ebook_metadata = self.config.get('ebook_metadata')
if not ebook.is_extra_format() and ebook_metadata:
# TODO: Try to use the calibre generated metadata file.
ebook_metadata_config = self.config.get('ebook_metadata')
if not ebook.is_extra_format():
with open(output_path, 'r+b') as file:
metadata = get_metadata(file, ebook.output_format)
ebook_title = metadata.title
if ebook.custom_title is not None:
ebook_title = ebook.custom_title
if ebook_metadata.get('lang_mark'):
if ebook_metadata_config.get('lang_mark'):
ebook_title = '%s [%s]' % (ebook_title, ebook.target_lang)
metadata.title = ebook_title
if ebook_metadata.get('lang_code'):
if ebook_metadata_config.get('lang_code'):
metadata.language = ebook.lang_code
subjects = ebook_metadata.get('subjects')
subjects = ebook_metadata_config.get('subjects')
metadata.tags += (subjects or []) + [
'Translated by Ebook Translator: '
'https://translator.bookfere.com']
# metadata.authors = ['bookfere.com']
# metadata.author_sort = 'bookfere.com'
# metadata.book_producer = 'Ebook Translator'
set_metadata(file, metadata, ebook.output_format)
else:
metadata = self.api.get_metadata(ebook.id)
ebook_title = ebook.title
if ebook.custom_title is not None:
ebook_title = ebook.custom_title
if ebook_metadata_config.get('lang_mark'):
ebook_title = '%s [%s]' % (ebook_title, ebook.target_lang)
metadata.title = ebook_title

if self.config.get('to_library'):
# with open(output_path, 'rb') as file:
Expand All @@ -266,15 +278,15 @@ def translate_done(self, job):
# os.remove(temp_file)
else:
dirname = os.path.dirname(output_path)
filename = '%s.%s' % (metadata.title, ebook.output_format)
filename = '%s.%s' % (ebook_title, ebook.output_format)
new_output_path = os.path.join(dirname, filename)
os.rename(output_path, new_output_path)
output_path = new_output_path

self.gui.status_bar.show_message(
job.description + ' ' + _('completed'), 5000)

openers = {'srt': open_path}
openers = {'srt': open_path, 'pgn': open_path}
opener = openers.get(ebook.input_format)

def callback(payload):
Expand All @@ -290,6 +302,6 @@ def callback(payload):
_('Ebook Translation Log'),
_('Translation Completed'),
_('The translation of "{}" was completed. '
'Do you want to open the book?').format(ebook.title),
'Do you want to open the book?').format(ebook_title),
log_is_file=True,
icon=self.icon)
69 changes: 22 additions & 47 deletions lib/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,6 @@ def __init__(self, placeholder, separator, position, merge_length=0):
self.elements = {}
self.originals = []

self.base_originals = []

def get_merge_length(self):
return self.merge_length

Expand Down Expand Up @@ -462,34 +460,35 @@ def prepare_original(self, elements):
attrs = element.get_attributes()
if not element.ignored:
self.elements[count] = element
self.base_originals.append(content)
count += 1
self.originals.append((
oid, md5, raw, content, element.ignored, attrs,
element.page_id))
return self.originals

def add_translations(self, paragraphs):
count = 0
def prepare_translation(self, paragraphs):
translations = {}
for paragraph in paragraphs:
if paragraph.original not in self.base_originals:
continue
element = self.elements.get(count)
if not element:
continue
translation = paragraph.translation
if translation:
element.add_translation(
translation, self.position, self.translation_lang,
self.original_color, self.translation_color)
self.elements.pop(count)
count += 1
translations[paragraph.original] = paragraph.translation
return translations

def add_translations(self, paragraphs):
translations = self.prepare_translation(paragraphs)
for eid, element in self.elements.copy().items():
if element.ignored:
self.elements.pop(eid)
for element in self.elements.values():
element.add_translation(
None, self.position, original_color=self.original_color)
continue
original = element.get_content()
translation = translations.get(original)
if translation is None:
element.add_translation(
None, self.position, original_color=self.original_color)
continue
element.add_translation(
None, self.position, original_color=self.original_color)
translation, self.position, self.translation_lang,
self.original_color, self.translation_color)
self.elements.pop(eid)


class ElementHandlerMerge(ElementHandler):
Expand All @@ -506,7 +505,6 @@ def prepare_original(self, elements):
element.set_column_gap(self.column_gap)
code = element.get_raw()
content = element.get_content()
self.base_originals.append(content)
content += self.separator
if len(txt + content) < self.merge_length:
raw += code + self.separator
Expand Down Expand Up @@ -555,41 +553,18 @@ def align_paragraph(self, paragraph):
offset = len(originals) - 1
translations = translations[:offset] + [
'\n\n'.join(translations[offset:])]
# for original in originals:
# if original and original not in self.base_originals:
# translations.pop(originals.index(original))
return list(zip(originals, translations))

def add_translations(self, paragraphs):
def prepare_translation(self, paragraphs):
translations = []
for paragraph in paragraphs:
translations.extend(self.align_paragraph(paragraph))
translations = dict(translations)
for eid, element in self.elements.copy().items():
if element.ignored:
element.add_translation(
None, self.position, original_color=self.original_color)
continue
original = element.get_content()
translation = translations.get(original)
if translation is None:
element.add_translation(
None, self.position, original_color=self.original_color)
continue
element.add_translation(
translation, self.position, self.translation_lang,
self.original_color, self.translation_color)
self.elements.pop(eid)
return dict(translations)


def get_srt_elements(path):
elements = []
try:
with open(path, 'r', newline=None) as f:
content = f.read().strip()
except Exception:
with open(path, 'rU') as f:
content = f.read().strip()
content = open_file(path)
for section in content.split('\n\n'):
lines = section.split('\n')
number = lines.pop(0)
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import sys
import codecs
import socket
import hashlib
from subprocess import Popen
Expand Down Expand Up @@ -113,7 +114,7 @@ def open_file(path):
with open(path, 'r', newline=None) as f:
content = f.read().strip()
except Exception:
with open(path, 'rU') as f:
with codecs.open(path, 'rU', encoding='utf-8') as f:
content = f.read().strip()
finally:
return content
Expand Down
Loading

0 comments on commit 143dc6f

Please sign in to comment.