Skip to content

Commit

Permalink
automatically set directionality if pre-defined in
Browse files Browse the repository at this point in the history
languages.py
show output format selection box in advanced mode window
some minor bug fixes
some improvements to language used in titles etc.
fixed minor typos
  • Loading branch information
seidnerj committed Dec 7, 2024
1 parent 3c661cd commit 07accfd
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 49 deletions.
113 changes: 69 additions & 44 deletions advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __init__(self, parent, ebook):
layout.addWidget(self.start_button)

def layout_format(self):
engine_class = get_engine_class()
widget = QWidget()
layout = QHBoxLayout(widget)
layout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -258,6 +259,13 @@ def layout_format(self):
input_layout.addWidget(input_format)
layout.addWidget(input_group)

output_group = QGroupBox(_('Output Format'))
output_layout = QGridLayout(output_group)
output_format = OutputFormat()
output_format.setFixedWidth(150)
output_layout.addWidget(output_format)
layout.addWidget(output_group)

source_group = QGroupBox(_('Source Language'))
source_layout = QVBoxLayout(source_group)
source_lang = SourceLang()
Expand All @@ -272,6 +280,35 @@ def layout_format(self):
target_layout.addWidget(target_lang)
layout.addWidget(target_group)

source_lang.refresh.emit(
engine_class.lang_codes.get('source'),
engine_class.config.get('source_lang'),
not issubclass(engine_class, CustomTranslate))

target_lang.refresh.emit(
engine_class.lang_codes.get('target'),
engine_class.config.get('target_lang'))

def change_input_format(_format):
self.ebook.set_input_format(_format)
change_input_format(input_format.currentText())
input_format.currentTextChanged.connect(change_input_format)

def change_output_format(_format):
self.ebook.set_output_format(_format)
change_output_format(output_format.currentText())
output_format.currentTextChanged.connect(change_output_format)

def change_source_lang(lang):
self.ebook.set_source_lang(lang)
change_source_lang(source_lang.currentText())
source_lang.currentTextChanged.connect(change_source_lang)

def change_target_lang(lang):
self.ebook.set_target_lang(lang)
change_target_lang(target_lang.currentText())
target_lang.currentTextChanged.connect(change_target_lang)

if self.ebook.input_format in extra_formats.keys():
encoding_group = QGroupBox(_('Encoding'))
encoding_layout = QVBoxLayout(encoding_group)
Expand All @@ -295,34 +332,17 @@ def change_encoding(encoding):
direction_layout.addWidget(direction_list)
layout.addWidget(direction_group)

def change_direction(index):
direction = direction_list.itemData(index)
self.ebook.set_target_direction(direction)
def change_direction(_index):
_direction = direction_list.itemData(_index)
self.ebook.set_target_direction(_direction)
direction_list.currentIndexChanged.connect(change_direction)

def change_input_format(format):
self.ebook.set_input_format(format)
change_input_format(input_format.currentText())
input_format.currentTextChanged.connect(change_input_format)

engine_class = get_engine_class()
source_lang.refresh.emit(
engine_class.lang_codes.get('source'),
engine_class.config.get('source_lang'),
not issubclass(engine_class, CustomTranslate))
target_lang.refresh.emit(
engine_class.lang_codes.get('target'),
engine_class.config.get('target_lang'))

def change_source_lang(lang):
self.ebook.set_source_lang(source_lang.currentText())
change_source_lang(source_lang.currentText())
source_lang.currentTextChanged.connect(change_source_lang)

def change_target_lang(lang):
self.ebook.set_target_lang(lang)
change_target_lang(target_lang.currentText())
target_lang.currentTextChanged.connect(change_target_lang)
engine_target_lange_codes = engine_class.lang_codes.get('target')
if engine_target_lange_codes is not None and self.ebook.target_lang in engine_target_lange_codes:
target_lang_code = engine_target_lange_codes[self.ebook.target_lang]
direction = engine_class.lang_codes_directionality.get(target_lang_code, 'auto')
index = direction_list.findData(direction)
direction_list.setCurrentIndex(index)

return widget

Expand Down Expand Up @@ -356,7 +376,7 @@ def __init__(self, parent, icon, worker, ebook):
self.cache = None
self.merge_enabled = False

self.prgress_step = 0
self.progress_step = 0
self.translate_all = False

self.editor_worker = EditorWorker()
Expand Down Expand Up @@ -612,7 +632,7 @@ def layout_table(self):
progress_bar.setVisible(False)

def write_progress():
value = progress_bar.value() + self.prgress_step
value = progress_bar.value() + self.progress_step
if value > progress_bar.maximum():
value = progress_bar.maximum()
progress_bar.setValue(value)
Expand Down Expand Up @@ -941,9 +961,12 @@ def layout_review(self):
translation_text.ensureCursorVisible)

def refresh_translation(paragraph):
raw_text.setPlainText(paragraph.raw.strip())
original_text.setPlainText(paragraph.original.strip())
translation_text.setPlainText(paragraph.translation)
# TODO: how can this happen and what should we do in case it does?
if paragraph is not None:
raw_text.setPlainText(paragraph.raw.strip())
original_text.setPlainText(paragraph.original.strip())
translation_text.setPlainText(paragraph.translation)

self.paragraph_sig.connect(refresh_translation)

self.trans_worker.start.connect(
Expand Down Expand Up @@ -1060,17 +1083,19 @@ def modify_translation():
self.editor_worker.show.connect(save_status.setText)

def save_translation():
save_button.setDisabled(True)
paragraph = self.table.current_paragraph()
translation = translation_text.toPlainText()
paragraph.translation = translation
paragraph.engine_name = self.current_engine.name
paragraph.target_lang = self.ebook.target_lang
self.table.row.emit(paragraph.row)
self.cache.update_paragraph(paragraph)
translation_text.setFocus(Qt.OtherFocusReason)
self.editor_worker.start[str].emit(
_('Your changes have been saved.'))
# TODO: how can this happen and what should we do in case it does?
if paragraph is not None:
save_button.setDisabled(True)
paragraph = self.table.current_paragraph()
translation = translation_text.toPlainText()
paragraph.translation = translation
paragraph.engine_name = self.current_engine.name
paragraph.target_lang = self.ebook.target_lang
self.table.row.emit(paragraph.row)
self.cache.update_paragraph(paragraph)
translation_text.setFocus(Qt.OtherFocusReason)
self.editor_worker.start[str].emit(_('Your changes have been saved.'))

save_button.clicked.connect(save_translation)
set_shortcut(save_button, 'save', save_translation, save_button.text())

Expand Down Expand Up @@ -1109,7 +1134,7 @@ def translate_all_paragraphs(self):
is_fresh = len(paragraphs) < 1
if is_fresh:
paragraphs = self.table.get_selected_paragraphs(False, True)
self.prgress_step = self.get_progress_step(len(paragraphs))
self.progress_step = self.get_progress_step(len(paragraphs))
if not self.translate_all:
message = _(
'Are you sure you want to translate all {:n} paragraphs?')
Expand All @@ -1124,7 +1149,7 @@ def translate_selected_paragraph(self):
if len(paragraphs) == self.table.rowCount():
self.translate_all_paragraphs()
else:
self.prgress_step = self.get_progress_step(len(paragraphs))
self.progress_step = self.get_progress_step(len(paragraphs))
self.trans_worker.translate.emit(paragraphs, True)

def install_widget_event(
Expand Down
6 changes: 4 additions & 2 deletions engines/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from mechanize._response import response_seek_wrapper as Response

from .base import Base
from .languages import anthropic as anthropic_languages
from .languages import lang_directionality
from .languages import anthropic
from .prompt_extensions import anthropic as anthropic_prompt_extension

try:
Expand All @@ -19,7 +20,8 @@
class ClaudeTranslate(Base):
name = 'Claude'
alias = 'Claude (Anthropic)'
lang_codes = Base.load_lang_codes(anthropic_languages)
lang_codes = Base.load_lang_codes(anthropic)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://api.anthropic.com/v1/messages'
api_version = '2023-06-01'
api_key_hint = 'sk-ant-xxxx'
Expand Down
2 changes: 2 additions & 0 deletions engines/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..lib.exception import BadApiKeyFormat

from .base import Base
from .languages import lang_directionality
from .languages import baidu


Expand All @@ -17,6 +18,7 @@ class BaiduTranslate(Base):
name = 'Baidu'
alias = _z('Baidu')
lang_codes = Base.load_lang_codes(baidu)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
api_key_hint = 'appid|appkey'
api_key_pattern = r'^[^\s:\|]+?[:\|][^\s:\|]+$'
Expand Down
4 changes: 4 additions & 0 deletions engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def load_lang_codes(cls, codes):
codes = {'source': codes, 'target': codes}
return codes

@classmethod
def load_lang_codes_directionality(cls, codes):
return codes

@classmethod
def get_source_code(cls, lang):
source_codes = cls.lang_codes.get('source')
Expand Down
3 changes: 3 additions & 0 deletions engines/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ..lib.utils import request

from .base import Base
from .languages import lang_directionality
from .languages import deepl


Expand All @@ -15,6 +16,7 @@ class DeeplTranslate(Base):
name = 'DeepL'
alias = 'DeepL'
lang_codes = Base.load_lang_codes(deepl)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://api-free.deepl.com/v2/translate'
usage_endpoint = 'https://api-free.deepl.com/v2/usage'
# api_key_hint = 'xxx-xxx-xxx:fx'
Expand Down Expand Up @@ -64,6 +66,7 @@ class DeeplFreeTranslate(Base):
alias = 'DeepL (Free)'
free = True
lang_codes = Base.load_lang_codes(deepl)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://www2.deepl.com/jsonrpc?client=chrome-extension,1.5.1'
need_api_key = False
placeholder = DeeplTranslate.placeholder
Expand Down
5 changes: 5 additions & 0 deletions engines/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..lib.utils import traceback_error

from .base import Base
from .languages import lang_directionality
from .languages import google, gemini


Expand All @@ -25,6 +26,7 @@ class GoogleFreeTranslate(Base):
alias = 'Google (Free)'
free = True
lang_codes = Base.load_lang_codes(google)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://translate.googleapis.com/translate_a/single'
need_api_key = False

Expand Down Expand Up @@ -154,6 +156,7 @@ class GoogleBasicTranslateADC(GoogleTranslateMixin, Base):
name = 'Google(Basic)ADC'
alias = 'Google (Basic) ADC'
lang_codes = Base.load_lang_codes(google)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://translation.googleapis.com/language/translate/v2'
api_key_hint = 'API key'
need_api_key = False
Expand Down Expand Up @@ -203,6 +206,7 @@ class GoogleAdvancedTranslate(GoogleTranslateMixin, Base):
name = 'Google(Advanced)'
alias = 'Google (Advanced) ADC'
lang_codes = Base.load_lang_codes(google)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://translation.googleapis.com/v3/projects/{}'
api_key_hint = 'PROJECT_ID'
need_api_key = False
Expand Down Expand Up @@ -237,6 +241,7 @@ class GeminiTranslate(Base):
name = 'Gemini'
alias = 'Gemini'
lang_codes = Base.load_lang_codes(gemini)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://generativelanguage.googleapis.com/v1/' \
'models/{}:{}?key={}'
need_api_key = True
Expand Down
11 changes: 10 additions & 1 deletion engines/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
'Hausa': 'ha',
'Hawaiian': 'haw',
'Hebrew': 'iw',
'Hebrew (with Niqqud)': 'iwn',
'Hebrew (with Niqqud)': 'iw',
'Hiligaynon': 'hil',
'Hindi': 'hi',
'Hmong': 'hmn',
Expand Down Expand Up @@ -1038,3 +1038,12 @@
'祖鲁语': 'zul',
'爪哇语': 'jav'
}

lang_directionality = {
'iw': 'rtl',
'ar': 'rtl',
'en': 'ltr',
'es': 'ltr',
'it': 'ltr',
'de': 'ltr'
}
4 changes: 3 additions & 1 deletion engines/microsoft.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from ..lib.utils import request

from .languages import microsoft
from .base import Base
from .languages import lang_directionality
from .languages import microsoft
from .openai import ChatgptTranslate


Expand All @@ -22,6 +23,7 @@ class MicrosoftEdgeTranslate(Base):
alias = 'Microsoft Edge (Free)'
free = True
lang_codes = Base.load_lang_codes(microsoft)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://api-edge.cognitive.microsofttranslator.com/translate'
need_api_key = False
access_info = None
Expand Down
2 changes: 2 additions & 0 deletions engines/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..lib.exception import UnsupportedModel

from .base import Base
from .languages import lang_directionality
from .languages import google


Expand All @@ -27,6 +28,7 @@ class ChatgptTranslate(Base):
name = 'ChatGPT'
alias = 'ChatGPT (OpenAI)'
lang_codes = Base.load_lang_codes(google)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://api.openai.com/v1/chat/completions'
# api_key_hint = 'sk-xxx...xxx'
# https://help.openai.com/en/collections/3808446-api-error-codes-explained
Expand Down
2 changes: 2 additions & 0 deletions engines/youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..lib.exception import BadApiKeyFormat

from .base import Base
from .languages import lang_directionality
from .languages import youdao


Expand All @@ -18,6 +19,7 @@ class YoudaoTranslate(Base):
name = 'Youdao'
alias = _z('Youdao')
lang_codes = Base.load_lang_codes(youdao)
lang_codes_directionality = Base.load_lang_codes_directionality(lang_directionality)
endpoint = 'https://openapi.youdao.com/api'
api_key_hint = 'appid|appsecret'
api_key_pattern = r'^[^\s:\|]+?[:\|][^\s:\|]+$'
Expand Down
4 changes: 3 additions & 1 deletion ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def show_advanced_translation(self):
ebooks = self.get_selected_ebooks()
if len(ebooks) < 1:
return self.alert.pop(
_('Please choose one single book.'), 'warning')
_('Please choose a book.'), 'warning')
window = CreateTranslationProject(self.gui, ebooks.first())
window.start_translation.connect(self.advanced_translation_window)
window.setModal(True)
window.setWindowTitle(
'%s - %s' % (_('Advanced Mode'), self.title))
window.setWindowTitle(self.title)
window.show()

Expand Down

0 comments on commit 07accfd

Please sign in to comment.