Skip to content

Commit

Permalink
feat!: Optimized adding translation feature. yihong0618#244
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Make previous cache deprecated.
  • Loading branch information
bookfere committed Mar 24, 2024
1 parent c744f97 commit 62cdbc8
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 193 deletions.
14 changes: 8 additions & 6 deletions advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from .lib.utils import uid
from .lib.config import get_config
from .lib.encodings import encoding_list
from .lib.cache import Paragraph, TranslationCache, get_cache
from .lib.cache import Paragraph, get_cache
from .lib.translation import get_engine_class, get_translator, get_translation
from .lib.element import get_element_handler, Extraction
from .lib.element import get_element_handler
from .lib.conversion import extract_item, extra_formats
from .engines.custom import CustomTranslate

Expand Down Expand Up @@ -84,8 +84,7 @@ def prepare_ebook_data(self):
encoding = self.ebook.encoding.lower()
cache_id = uid(
input_path + self.engine_class.name + self.ebook.target_lang
+ merge_length + encoding + TranslationCache.__version__
+ Extraction.__version__)
+ merge_length + encoding)
cache = get_cache(cache_id)

if cache.is_fresh() or not cache.is_persistence():
Expand Down Expand Up @@ -765,8 +764,11 @@ def disable_translation_text():
disable_translation_text)

def auto_open_close_splitter():
size = 0 if splitter.sizes()[0] > 0 else 1
splitter.setSizes([size, 1, 1])
if splitter.sizes()[0] > 0:
sizes = [0] + [int(splitter.height() / 2)] * 2
else:
sizes = [int(splitter.height() / 3)] * 3
splitter.setSizes(sizes)
self.install_widget_event(
splitter, splitter.handle(1), QEvent.MouseButtonDblClick,
auto_open_close_splitter)
Expand Down
1 change: 0 additions & 1 deletion lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def cache_path():


class TranslationCache:
__version__ = '20230608'
fresh = True
dir_path = cache_path()
cache_path = os.path.join(dir_path, 'cache')
Expand Down
20 changes: 11 additions & 9 deletions lib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

from .config import get_config
from .utils import sep, uid, open_path, open_file
from .cache import get_cache, TranslationCache
from .cache import get_cache
from .element import (
Extraction, get_element_handler, get_srt_elements, get_toc_elements,
get_page_elements, get_metadata_elements, get_pgn_elements)
get_element_handler, get_srt_elements, get_toc_elements, get_page_elements,
get_metadata_elements, get_pgn_elements)
from .translation import get_translator, get_translation
from .exception import ConversionAbort


load_translations()

log = Log()
Expand Down Expand Up @@ -78,8 +79,9 @@ def convert(self, oeb, output_path, input_plugin, opts, log):
plumber.run()


def convert_srt(input_path, output_path, translation, element_handler, cache,
debug_info, encoding, notification):
def convert_srt(
input_path, output_path, translation, element_handler, cache,
debug_info, encoding, notification):
log.info('Translating subtitles content... (this will take a while)')
log.info(debug_info)

Expand All @@ -102,8 +104,9 @@ def convert_srt(input_path, output_path, translation, element_handler, cache,
log(_('The translation of the subtitles file was completed.'))


def convert_pgn(input_path, output_path, translation, element_handler, cache,
debug_info, encoding, notification):
def convert_pgn(
input_path, output_path, translation, element_handler, cache,
debug_info, encoding, notification):
log.info('Translating PGN content... (this may be take a while)')
log.info(debug_info)

Expand Down Expand Up @@ -196,8 +199,7 @@ def convert_item(
if encoding.lower() != 'utf-8':
_encoding = encoding.lower()
cache_id = uid(
input_path + translator.name + target_lang + merge_length + _encoding
+ TranslationCache.__version__ + Extraction.__version__)
input_path + translator.name + target_lang + merge_length + _encoding)
cache = get_cache(cache_id)
cache.set_cache_only(cache_only)
cache.set_info('title', ebook_title)
Expand Down
Loading

0 comments on commit 62cdbc8

Please sign in to comment.