Skip to content

Commit

Permalink
feat: 分割位置保存在本地
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Aug 25, 2024
1 parent ce928e4 commit 6a0dfd8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 55 deletions.
4 changes: 4 additions & 0 deletions data/cool.ldr.lfy.in.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<default>0</default>
<description>The currently selected translation language</description>
</key>
<key type="i" name="translate-paned-position">
<default>-1</default>
<description>Split position of translation page</description>
</key>
<key type="s" name="server-sk-baidu">
<default>"app_id | secret_key"</default>
<description>Baidu app_id and secret_key, used in the middle | split</description>
Expand Down
14 changes: 13 additions & 1 deletion lfy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def lang_selected_n(self):
def lang_selected_n(self, n):
self.set_int('lang-selected-n', n)

@property
def translate_paned_position(self):
"""翻译页面分割位置
Returns:
int: 如 0
"""
return self.get_int('translate-paned-position')

@translate_paned_position.setter
def translate_paned_position(self, n):
self.set_int('translate-paned-position', n)

@property
def server_sk_baidu(self):
"""选择翻译的服务密钥,只是一个字符串,自己解析和保存用 | 分割
Expand Down Expand Up @@ -181,7 +194,6 @@ def auto_check_update(self):
"""
return self.get_boolean('auto-check-update')


@property
def notify_translation_results(self):
"""翻译以后自动发送结果
Expand Down
42 changes: 26 additions & 16 deletions lfy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ class TranslateWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)

self.paned_position = -1

self.setting = Settings.get()

# 可能包含上次的追加内容
self.last_text = ""
# 这次复制的
Expand All @@ -91,31 +90,39 @@ def __init__(self, **kwargs):

self.connect('unrealize', self.save_settings)

self.paned_position = self.setting.translate_paned_position
if self.paned_position > 0:
self.gp_translate.set_position(self.paned_position)

def set_paned_position(self, p):
"""设置或恢复,原文字和翻译的比例
Args:
p (_type_): 设置的位置
"""
if self.paned_position < 0:
self.paned_position = self.gp_translate.get_position()
else:
p = self.paned_position
# 标记设置过了
self.paned_position = -1
self.gp_translate.set_position(p)

def reset_paned_position(self):
"""重置原文字和翻译的比例
"""
height = self.get_allocated_height()
h_reset = height / 5 * 2
h_now = self.gp_translate.get_position()
if h_now != h_reset:
self.paned_position = h_now
self.gp_translate.set_position(h_reset)
else:
self.gp_translate.set_position(self.paned_position)
self.set_paned_position(self.get_allocated_height() / 5 * 2)

def up_paned_position(self):
"""只看翻译
"""
self.gp_translate.set_position(0)
self.set_paned_position(0)

def down_paned_position(self):
"""只看原文字
"""
height = self.get_allocated_height()
self.gp_translate.set_position(height)


self.set_paned_position(height)

def save_settings(self, _a):
"""_summary_
Expand All @@ -124,14 +131,16 @@ def save_settings(self, _a):
_a (TranslateWindow): _description_
"""
if not self.is_maximized():
Settings.get().window_size = self.get_default_size()
self.setting.window_size = self.get_default_size()

i = self.dd_server.get_selected()
j = self.dd_lang.get_selected()
self.setting.server_selected_key = get_server(i).key
n = get_lang(i, j).n
self.setting.lang_selected_n = n

self.setting.translate_paned_position = self.gp_translate.get_position()

@Gtk.Template.Callback()
def _on_server_changed(self, drop_down, _a):
# 初始化,会不断调用这个
Expand Down Expand Up @@ -244,7 +253,8 @@ def update_ui(self, s="", ocr=False):
if Settings.get().notify_translation_results:
if len(s) > 250:
s = s[:250]
nt_title = f"{self.tran_server.name} " + _("Translation completed")
nt_title = f"{self.tran_server.name} " + \
_("Translation completed")
Notify.Notification.new(nt_title, s).show()
except TypeError as e:
error_msg = _("something error:")
Expand Down
108 changes: 70 additions & 38 deletions po/zh_CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cool.ldr.lfy\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2024-07-04 14:50+0800\n"
"POT-Creation-Date: 2024-08-25 17:52+0800\n"
"PO-Revision-Date: 2023-11-12 21:02+0800\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: Chinese (simplified) <[email protected]>\n"
Expand All @@ -18,11 +18,11 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"

#: data/cool.ldr.lfy.in.in.desktop:3 data/cool.ldr.lfy.in.appdata.xml:4
#: lfy/main.py:33 lfy/main.py:88 data/resources/translate.ui:112
#: lfy/main.py:33 lfy/main.py:99 data/resources/translate.ui:129
msgid "lfy"
msgstr "兰译"

#: data/cool.ldr.lfy.in.metainfo.xml:6 data/cool.ldr.lfy.in.appdata.xml:5
#: data/cool.ldr.lfy.in.metainfo.xml:6
msgid "Translation software for gnome"
msgstr "为gnome设计的翻译软件"

Expand Down Expand Up @@ -99,6 +99,11 @@ msgstr "VPN地址"
msgid "Which services are available in compare model"
msgstr "在比较模式下哪些服务被选中"

#: data/cool.ldr.lfy.in.appdata.xml:5
#, fuzzy
msgid "Translation software for read paper"
msgstr "为gnome设计的翻译软件"

#: data/cool.ldr.lfy.in.appdata.xml:15
#, fuzzy
msgid "Translation homepage"
Expand All @@ -124,73 +129,88 @@ msgid "About"
msgstr "_关于 兰译"

#: data/cool.ldr.lfy.in.appdata.xml:41
#, fuzzy
msgid "Add aliyun/huoshan Translate for free"
msgstr "添加阿里云/火山翻译(需密钥、但免费)"

#: data/cool.ldr.lfy.in.appdata.xml:42
msgid "Support notification after translation"
msgstr "支持通知翻译内容"

#: data/cool.ldr.lfy.in.appdata.xml:43
msgid "Supports comparing multiple translations at the same time"
msgstr "支持同时比较多个翻译"

#: data/cool.ldr.lfy.in.appdata.xml:50
msgid "Add Bing Translate for free"
msgstr "添加必应翻译(无需密钥)"

#: data/cool.ldr.lfy.in.appdata.xml:48
#: data/cool.ldr.lfy.in.appdata.xml:57
#, fuzzy
msgid "Fix Google Translate"
msgstr "翻译"

#: data/cool.ldr.lfy.in.appdata.xml:49
#: data/cool.ldr.lfy.in.appdata.xml:58
msgid "Improve translation performance"
msgstr "提高翻译性能"

#: data/cool.ldr.lfy.in.appdata.xml:50
#: data/cool.ldr.lfy.in.appdata.xml:59
msgid "Add Italian language and support Italian translation"
msgstr "添加意大利语言,支持意大利语翻译"

#: data/cool.ldr.lfy.in.appdata.xml:51
#: data/cool.ldr.lfy.in.appdata.xml:60
msgid ""
"Optimize the structure of translation services and make it more convenient "
"to create third-party translation services"
msgstr "优化翻译服务结构,打造第三方翻译服务更便捷"

#: data/cool.ldr.lfy.in.appdata.xml:58
#: data/cool.ldr.lfy.in.appdata.xml:67
msgid "add shortcut"
msgstr "添加快捷键"

#: data/cool.ldr.lfy.in.appdata.xml:59
#: data/cool.ldr.lfy.in.appdata.xml:68
msgid "Automatically check for updates"
msgstr "软件打开时,自动检查更新"

#: data/cool.ldr.lfy.in.appdata.xml:65
#: data/cool.ldr.lfy.in.appdata.xml:74
msgid "Basic functions completed"
msgstr "基础功能完成"

#: data/cool.ldr.lfy.in.appdata.xml:70
#: data/cool.ldr.lfy.in.appdata.xml:79
msgid "yuh"
msgstr "余航"

#: lfy/main.py:94
#: lfy/main.py:105
msgid "translator_credits"
msgstr "yuh <[email protected]>, 2023-2023"

#: lfy/main.py:95
#: lfy/main.py:106
msgid "A translation app for GNOME."
msgstr "一个翻译软件,为gnome桌面设计"

#: lfy/main.py:120 data/resources/help-overlay.ui:25
#: lfy/main.py:131 data/resources/help-overlay.ui:20
#: data/resources/help-overlay.ui:27 data/resources/help-overlay.ui:34
#: data/resources/help-overlay.ui:53
msgid "Copy detected, translate immediately"
msgstr "检测到复制,立刻翻译"

#: lfy/main.py:123
#: lfy/main.py:134
msgid "Copy detected, not automatically translated"
msgstr "检测到复制,不自动翻译"

#: lfy/main.py:147
#: lfy/main.py:158
msgid "Next translation not remove line breaks"
msgstr "下次翻译不移除换行"

#: lfy/main.py:148
#: lfy/main.py:159
msgid "Next translation remove line breaks"
msgstr "下次翻译移除换行"

#: lfy/main.py:158
#: lfy/main.py:169
msgid "Next translation without splicing text"
msgstr "下次翻译不拼接文本"

#: lfy/main.py:159
#: lfy/main.py:170
msgid "Next translation splicing text"
msgstr "下次翻译拼接文本"

Expand All @@ -202,22 +222,22 @@ msgstr "对比"
msgid "It takes effect when you restart lfy"
msgstr "重新打开lfy时生效"

#: lfy/translate.py:187 lfy/translate.py:220 lfy/api/server/baidu.py:42
#: lfy/translate.py:217 lfy/translate.py:250 lfy/api/server/baidu.py:42
#: lfy/api/server/baidu.py:203 lfy/api/server/tencent.py:149
#: lfy/api/server/aliyun.py:98 lfy/api/server/com.py:28
#: lfy/api/server/huoshan.py:136
msgid "something error:"
msgstr "一些错误发生:"

#: lfy/translate.py:203
#: lfy/translate.py:233
msgid "OCRing.."
msgstr "OCR识别中.."

#: lfy/translate.py:205
#: lfy/translate.py:235
msgid "Translating.."
msgstr "翻译中……"

#: lfy/translate.py:217
#: lfy/translate.py:247
msgid "Translation completed"
msgstr "翻译完成"

Expand Down Expand Up @@ -318,39 +338,47 @@ msgstr "请输入 app_id 和 secret_key,类似:"
msgid "huoshan"
msgstr "火山"

#: data/resources/translate.ui:60
#: data/resources/translate.ui:71
msgid "Remove symbols such as line breaks"
msgstr "移除换行符"

#: data/resources/translate.ui:66 data/resources/help-overlay.ui:32
#: data/resources/help-overlay.ui:39
#: data/resources/translate.ui:78 data/resources/help-overlay.ui:60
#: data/resources/help-overlay.ui:67
msgid "Splice the next copied text with the previous text"
msgstr "将下一个复制的文本与上一个文本拼接"

#: data/resources/translate.ui:72
#: data/resources/translate.ui:85
msgid "translate"
msgstr "翻译"

#: data/resources/translate.ui:99
#: data/resources/translate.ui:116
msgid "Menu"
msgstr "菜单"

#: data/resources/translate.ui:123 data/resources/help-overlay.ui:24
#: data/resources/translate.ui:140 data/resources/help-overlay.ui:52
msgid "Copy to translate"
msgstr "复制即翻译"

#: data/resources/translate.ui:129
#: data/resources/translate.ui:146
msgid "_Preferences"
msgstr "_设置"

#: data/resources/translate.ui:133
#: data/resources/translate.ui:150
msgid "_Keyboard Shortcuts"
msgstr "_快捷键"

#: data/resources/translate.ui:137
#: data/resources/translate.ui:154
msgid "_About lfy"
msgstr "_关于 兰译"

#: data/resources/translate.ui:160
msgid "Reset/Restore UI"
msgstr "重置/恢复UI"

#: data/resources/translate.ui:164 data/resources/help-overlay.ui:26
msgid "Hide the original text"
msgstr "隐藏源文本"

#: data/resources/server-preferences.ui:13
msgid "Translate Server"
msgstr "翻译服务"
Expand Down Expand Up @@ -400,29 +428,33 @@ msgstr "软件打开时,自动检查更新"
msgid "Notify translation results"
msgstr "通知翻译结果"

#: data/resources/help-overlay.ui:14
#: data/resources/help-overlay.ui:33
msgid "Hide the translated text"
msgstr "隐藏翻译文本"

#: data/resources/help-overlay.ui:42
msgctxt "shortcut window"
msgid "General"
msgstr "通用"

#: data/resources/help-overlay.ui:18
#: data/resources/help-overlay.ui:46
msgid "Translate"
msgstr "翻译"

#: data/resources/help-overlay.ui:31
#: data/resources/help-overlay.ui:59
msgid "Splice Text"
msgstr "拼接文本"

#: data/resources/help-overlay.ui:38
#: data/resources/help-overlay.ui:66
msgid "Remove line breaks"
msgstr "移除换行符"

#: data/resources/help-overlay.ui:45
#: data/resources/help-overlay.ui:73
msgctxt "shortcut window"
msgid "Quit"
msgstr "退出"

#: data/resources/help-overlay.ui:51
#: data/resources/help-overlay.ui:79
msgctxt "shortcut window"
msgid "Show Shortcuts"
msgstr "显示快捷键"
Expand Down

0 comments on commit 6a0dfd8

Please sign in to comment.