Skip to content

Commit

Permalink
fix: cb get_formats is_text
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Sep 5, 2024
1 parent 4b09ee7 commit d4934cd
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 27 deletions.
2 changes: 1 addition & 1 deletion data/resources/lfy.cmb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
(2,88,"GtkWidget","tooltip-text","Read the JSON configuration of the clipboard, then import it, and some of the configurations will take effect after reopening the software",1,None,None,None,None,None,None,None,None),
(2,88,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None),
(2,89,"AdwActionRow","subtitle","all",None,None,None,None,None,None,None,None,None),
(2,89,"AdwPreferencesRow","title","Which services are available in compare model",1,None,None,None,None,None,None,None,None),
(2,89,"AdwPreferencesRow","title","Compare model",1,None,None,None,None,None,None,None,None),
(2,90,"GtkMenuButton","popover",None,None,None,None,None,91,None,None,None,None),
(2,90,"GtkWidget","tooltip-text","Which services are available in compare model",1,None,None,None,None,None,None,None,None),
(2,90,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None),
Expand Down
2 changes: 1 addition & 1 deletion data/resources/preference.ui
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<child>
<object class="AdwActionRow" id="aar_compare">
<property name="subtitle">all</property>
<property name="title" translatable="yes">Which services are available in compare model</property>
<property name="title" translatable="yes">Compare model</property>
<child>
<object class="GtkMenuButton" id="gbtn_compare">
<property name="popover">
Expand Down
6 changes: 6 additions & 0 deletions lfy/api/constant.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'常量,尽可能不引用其他文件'
from lfy.api.server import (Server, aliyun, baidu, bing, com, google, huoshan,
tencent)

Expand All @@ -10,3 +11,8 @@
aliyun.AliYunServer(),
huoshan.HuoShanServer(),
]


NO_TRANSLATED_TXTS = [
"\"server-sk-",
]
16 changes: 16 additions & 0 deletions lfy/api/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ def s2ks(s):
a = app_id.strip()
b = secret_key.strip()
return a, b


def is_text(cf):
"""cb.get_formats()
Args:
cf (GdkContentFormats): Gdk.Clipboard.get_formats
Returns:
_type_: _description_
"""
if cf.contain_mime_type("text/plain") \
or cf.contain_mime_type("text/html") \
or cf.contain_mime_type("text/plain;charset=utf-8"):
return True
return False
6 changes: 4 additions & 2 deletions lfy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from gi.repository import Adw, Gdk, Gio, GLib, Gtk, Notify

from lfy import PACKAGE_URL, PACKAGE_URL_BUG
from lfy.api.utils import is_text
from lfy.api.utils.check_update import main as check_update
from lfy.preference import PreferenceWindow
from lfy.settings import Settings
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, app_id, version):
threading.Thread(target=self.find_update, daemon=True).start()

def do_activate(self, s="", ocr=False):
"""_summary_
"""翻译
Args:
s (str, optional): _description_. Defaults to "".
Expand Down Expand Up @@ -225,7 +226,8 @@ def save_img(cb2, res):
if span < 1 or len(cf.get_mime_types()) == 0:
return

if cf.contain_mime_type("text/plain"):
print(cf.get_mime_types())
if is_text(cf):
self.last_clip = time.time()
cb.read_text_async(None, on_active_copy)
elif cf.contain_mime_type('image/png'):
Expand Down
14 changes: 12 additions & 2 deletions lfy/preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from lfy.api import get_server_names_api_key, get_servers_api_key
from lfy.api.constant import SERVERS
from lfy.api.server import Server
from lfy.api.utils import is_text
from lfy.api.utils.bak import backup_gsettings, restore_gsettings
from lfy.settings import Settings
from lfy.widgets.server_preferences import ServerPreferences
Expand Down Expand Up @@ -75,13 +76,22 @@ def _init_pop_compare(self):
def _import_config(self, _b):

def on_active_copy(cb2, res):
s = restore_gsettings(cb2.read_text_finish(res))
s = cb2.read_text_finish(res)
print(s)
s = restore_gsettings(s)
if len(s) == 0:
s = _("It takes effect when you restart lfy")
self.get_root().add_toast(Adw.Toast.new(s))

if self.cb.get_formats().contain_mime_type("text/plain"):
cf = self.cb.get_formats()

print(cf.get_mime_types())

if is_text(cf):
self.cb.read_text_async(None, on_active_copy)
else:
notice_s = _("The clipboard format is incorrect")
self.get_root().add_toast(Adw.Toast.new(notice_s))

@Gtk.Template.Callback()
def _export_config(self, _b):
Expand Down
16 changes: 14 additions & 2 deletions lfy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lfy.api import (create_server, create_server_ocr, get_lang,
get_lang_names, get_server, get_server_names, lang_n2j,
server_key2i)
from lfy.api.constant import NO_TRANSLATED_TXTS
from lfy.api.server import Server
from lfy.settings import Settings
from lfy.widgets.theme_switcher import ThemeSwitcher
Expand Down Expand Up @@ -178,13 +179,24 @@ def update(self, text, reload=False, del_wrapping=True):
text (_type_): _description_
reload (bool, optional): _description_. Defaults to False.
"""
buffer_from = self.tv_from.get_buffer()

if len(text) == 0:
buffer_from.set_text(
_("This time the content is blank and will not be translated"))
return

# 导出或者导入的配置包含密钥,不翻译
if "\"server-sk-" in text:
s_ntt = _(
"This time the content contains private information and is not translated")
ss_ntt = []
for ntt in NO_TRANSLATED_TXTS:
if ntt in text:
ss_ntt.append(ntt)
if len(ss_ntt) > 0:
buffer_from.set_text(f"{s_ntt}:\n{str(ss_ntt)}")
return

buffer_from = self.tv_from.get_buffer()
if not reload:
if self.last_text_one == text or self.is_tv_copy:
self.is_tv_copy = False
Expand Down
54 changes: 35 additions & 19 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-09-03 12:10+0800\n"
"POT-Creation-Date: 2024-09-05 15:02+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,7 +18,7 @@ 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:95 data/resources/translate.ui:130
#: lfy/main.py:34 lfy/main.py:96 data/resources/translate.ui:130
msgid "lfy"
msgstr "兰译"

Expand Down Expand Up @@ -94,8 +94,7 @@ msgstr "请输入 app_id 和 secret_key,类似:"
msgid "VPN address and port"
msgstr "VPN地址"

#: data/cool.ldr.lfy.in.gschema.xml:71 data/resources/preference.ui:76
#: data/resources/preference.ui:90
#: data/cool.ldr.lfy.in.gschema.xml:71 data/resources/preference.ui:90
msgid "Which services are available in compare model"
msgstr "在比较模式下哪些服务被选中"

Expand Down Expand Up @@ -173,68 +172,81 @@ msgstr "基础功能完成"
msgid "yuh"
msgstr "余航"

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

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

#: lfy/main.py:127 data/resources/help-overlay.ui:20
#: lfy/main.py:128 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:130
#: lfy/main.py:131
msgid "Copy detected, not automatically translated"
msgstr "检测到复制,不自动翻译"

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

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

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

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

#: lfy/preference.py:56 lfy/api/server/com.py:64
#: lfy/preference.py:57 lfy/api/server/com.py:64
msgid "compare"
msgstr "对比"

#: lfy/preference.py:80 lfy/preference.py:113 lfy/preference.py:136
#: lfy/preference.py:83 lfy/preference.py:123 lfy/preference.py:146
msgid "It takes effect when you restart lfy"
msgstr "重新打开lfy时生效"

#: lfy/preference.py:91
#: lfy/preference.py:93
msgid "The clipboard format is incorrect"
msgstr "剪贴板文本格式不正确"

#: lfy/preference.py:101
msgid "Configuration data has been exported to the clipboard"
msgstr "配置数据已导出到剪贴板"

#: lfy/translate.py:229 lfy/translate.py:263 lfy/api/server/baidu.py:42
#: lfy/translate.py:186
msgid "This time the content is blank and will not be translated"
msgstr "本次内容为空,不翻译"

#: lfy/translate.py:191
msgid ""
"This time the content contains private information and is not translated"
msgstr "本次内容包含隐私信息,不翻译"

#: lfy/translate.py:241 lfy/translate.py:275 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:245
#: lfy/translate.py:257
msgid "OCRing.."
msgstr "OCR识别中.."

#: lfy/translate.py:247
#: lfy/translate.py:259
msgid "Translating.."
msgstr "翻译中……"

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

Expand Down Expand Up @@ -434,6 +446,10 @@ msgstr "自动检查更新"
msgid "Notify translation results"
msgstr "通知翻译结果"

#: data/resources/preference.ui:76
msgid "Compare model"
msgstr "对比模式"

#: data/resources/preference.ui:102
msgid "backup/restore the settings to/from the clipboard, edit or backup it"
msgstr "将设置备份到剪贴板或从剪贴板恢复、编辑或备份"
Expand Down

0 comments on commit d4934cd

Please sign in to comment.