From 4dd72dfe4e2660a6dc0f6dbc360ea8055ee5d867 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Sun, 17 May 2020 13:07:57 +0800 Subject: [PATCH 1/6] fix youdao translator 113 exception issue: #491 --- .../action/TranslateDocumentationAction.kt | 21 +++++++++++-------- .../TranslatingDocumentationProvider.kt | 10 ++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt index 858ef7ba4..5fadfcb3c 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt @@ -31,7 +31,6 @@ import org.jsoup.nodes.Document import org.jsoup.nodes.Element import java.awt.Dimension import java.io.StringReader -import java.lang.Exception import java.lang.ref.WeakReference import javax.swing.text.html.HTMLDocument import javax.swing.text.html.HTMLEditorKit @@ -182,9 +181,9 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { // 删除多余的 `p` 标签。 body.selectFirst(CSS_QUERY_CONTENT) - ?.nextElementSibling() - ?.takeIf { it.isEmptyParagraph() } - ?.remove() + ?.nextElementSibling() + ?.takeIf { it.isEmptyParagraph() } + ?.remove() val preElements = body.select(TAG_PRE) preElements.forEachIndexed { index, element -> @@ -193,7 +192,9 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { // 翻译内容会带有原文与译文,分号包在 `i` 标签和 `b` 标签内,因此替换掉这两个标签以免影响到翻译后的处理。 val content = body.html().replaceTag(TAG_B, TAG_STRONG).replaceTag(TAG_I, TAG_EM) - val translation = translateDocumentation(content, Lang.AUTO, primaryLanguage).translation ?: "" + val translation = + if (content.isBlank()) "" + else translateDocumentation(content, Lang.AUTO, primaryLanguage).translation ?: "" body.html(translation) // 去除原文标签。 @@ -228,14 +229,16 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { val htmlDocument = HTMLDocument().also { HTML_KIT.read(StringReader(body.html()), it, 0) } val formatted = htmlDocument.getText(0, htmlDocument.length).trim() - val translation = translateDocumentation(formatted, Lang.AUTO, primaryLanguage).translation ?: "" + val translation = + if (formatted.isEmpty()) "" + else translateDocumentation(formatted, Lang.AUTO, primaryLanguage).translation ?: "" val newBody = Element("body") definition?.let { newBody.appendChild(it) } Element("div") - .addClass("content") - .append(translation.replace("\n", "
")) - .let { newBody.appendChild(it) } + .addClass("content") + .append(translation.replace("\n", "
")) + .let { newBody.appendChild(it) } body.replaceWith(newBody) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/provider/TranslatingDocumentationProvider.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/provider/TranslatingDocumentationProvider.kt index 789f3b862..e762196ae 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/provider/TranslatingDocumentationProvider.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/provider/TranslatingDocumentationProvider.kt @@ -35,7 +35,7 @@ class TranslatingDocumentationProvider : DocumentationProviderEx() { } companion object { - private val recursion = ThreadLocal.withInitial{0} + private val recursion = ThreadLocal.withInitial { 0 } private fun nullIfRecursive(computation: () -> T?): T? { if (recursion.get() > 0) @@ -54,7 +54,7 @@ class TranslatingDocumentationProvider : DocumentationProviderEx() { private var lastTranslation: TranslationTask? = null private fun translate(text: String?): String? { - text ?: return null + if (text.isNullOrEmpty()) return null val lastTask = lastTranslation @@ -85,11 +85,9 @@ class TranslatingDocumentationProvider : DocumentationProviderEx() { ProgressManager.checkCanceled() try { return promise.blockingGet(timeToBlockMs) - } - catch (t: TimeoutException) { + } catch (t: TimeoutException) { //ignore - } - catch (e: Throwable) { + } catch (e: Throwable) { TranslateDocumentationAction.logAndShowWarning(e, null) return null } From 28b37032343663e575744f7c22148b761961d135 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Sun, 17 May 2020 14:38:22 +0800 Subject: [PATCH 2/6] clean up --- .../cn/yiiguxing/plugin/translate/Settings.kt | 1 - .../plugin/translate/TranslationBundle.kt | 1 - .../action/ExclusiveTranslateAction.kt | 1 - .../action/InclusiveTranslateAction.kt | 1 - .../action/ShowWordOfTheDayAction.kt | 2 - .../action/TranslateAndReplaceAction.kt | 1 - .../action/TranslateQuickDocAction.kt | 1 - .../action/TranslateTextComponentAction.kt | 1 - .../plugin/translate/trans/Languages.kt | 109 +++++++++++++++++- .../plugin/translate/ui/TranslationPane.kt | 1 - .../ui/settings/BaiduAppKeySettingsPanel.kt | 5 +- .../translate/ui/settings/SettingsPanel.kt | 2 - .../ui/settings/YoudaoAppKeySettingsPanel.kt | 5 +- .../ui/wordbook/WordOfTheDayDialog.kt | 2 - 14 files changed, 111 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt index 9ff4cb019..39c9ed689 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt @@ -230,7 +230,6 @@ enum class TTSSource(val displayName: String) { TRANSLATION(message("settings.item.translation")) } -@Suppress("InvalidBundleOrProperty") enum class TargetLanguageSelection(val displayName: String) { DEFAULT(message("default")), PRIMARY_LANGUAGE(message("settings.item.primaryLanguage")), diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/TranslationBundle.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/TranslationBundle.kt index 85e4defcc..efe5e6274 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/TranslationBundle.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/TranslationBundle.kt @@ -3,7 +3,6 @@ package cn.yiiguxing.plugin.translate import com.intellij.AbstractBundle import org.jetbrains.annotations.PropertyKey -@Suppress("InvalidBundleOrProperty") const val BUNDLE = "messages.TranslationBundle" object TranslationBundle : AbstractBundle(BUNDLE) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ExclusiveTranslateAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ExclusiveTranslateAction.kt index e8fbf63a1..7da962ca2 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ExclusiveTranslateAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ExclusiveTranslateAction.kt @@ -9,7 +9,6 @@ import cn.yiiguxing.plugin.translate.util.SelectionMode class ExclusiveTranslateAction : TranslateAction(false) { init { - @Suppress("InvalidBundleOrProperty") templatePresentation.description = message("action.description.exclusive") } diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/InclusiveTranslateAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/InclusiveTranslateAction.kt index 4c825452a..91babd3f3 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/InclusiveTranslateAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/InclusiveTranslateAction.kt @@ -9,7 +9,6 @@ import cn.yiiguxing.plugin.translate.util.SelectionMode class InclusiveTranslateAction : TranslateAction(false) { init { - @Suppress("InvalidBundleOrProperty") templatePresentation.description = message("action.description.inclusive") } diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ShowWordOfTheDayAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ShowWordOfTheDayAction.kt index 8f784012f..5b0d72373 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ShowWordOfTheDayAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/ShowWordOfTheDayAction.kt @@ -1,5 +1,3 @@ -@file:Suppress("InvalidBundleOrProperty") - package cn.yiiguxing.plugin.translate.action import cn.yiiguxing.plugin.translate.message diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateAndReplaceAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateAndReplaceAction.kt index 010d684e2..862c8e49c 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateAndReplaceAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateAndReplaceAction.kt @@ -175,7 +175,6 @@ class TranslateAndReplaceAction : AutoSelectAction(true, NON_WHITESPACE_CONDITIO } val index = languages.indexOf(appStorage.lastReplacementTargetLanguage) - @Suppress("InvalidBundleOrProperty") val step = object : SpeedSearchListPopupStep(languages, title = message("title.targetLanguage")) { override fun getTextFor(value: Lang): String = value.langName override fun onChosen(selectedValue: Lang, finalChoice: Boolean): PopupStep<*>? { diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateQuickDocAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateQuickDocAction.kt index 0e4fa007b..d1691c65c 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateQuickDocAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateQuickDocAction.kt @@ -17,7 +17,6 @@ class TranslateQuickDocAction : AnAction(), DumbAware, HintManagerImpl.ActionToI init { isEnabledInModalContext = true - @Suppress("InvalidBundleOrProperty") templatePresentation.description = message("action.description.quickDoc") } diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateTextComponentAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateTextComponentAction.kt index 8fa9610f5..d14e5833c 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateTextComponentAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateTextComponentAction.kt @@ -16,7 +16,6 @@ class TranslateTextComponentAction : TextComponentEditorAction(Handler()), HintM init { isEnabledInModalContext = true - @Suppress("InvalidBundleOrProperty") templatePresentation.description = message("action.description.textComponent") } diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/Languages.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/Languages.kt index 3f18b8adc..769f86876 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/Languages.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/Languages.kt @@ -6,7 +6,6 @@ import com.intellij.util.xmlb.annotations.Tag import org.jetbrains.annotations.PropertyKey import java.util.* -@Suppress("InvalidBundleOrProperty") private const val LANGUAGE_BUNDLE = "messages.LanguageBundle" private object LanguageBundle : AbstractBundle(LANGUAGE_BUNDLE) @@ -22,7 +21,7 @@ data class LanguagePair( /** * 语言 */ -@Suppress("InvalidBundleOrProperty", "SpellCheckingInspection", "unused") +@Suppress("SpellCheckingInspection", "unused") enum class Lang( @PropertyKey(resourceBundle = LANGUAGE_BUNDLE) langNameKey: String, @@ -33,216 +32,322 @@ enum class Lang( /** 自动检测 */ AUTO("auto", "auto"), + /** 中文 */ CHINESE("chinese", "zh-CN", "zh-CHS", "zh"), + /** 英语 */ ENGLISH("english", "en"), + /** 中文(繁体) */ CHINESE_TRADITIONAL("chinese.traditional", "zh-TW", baiduCode = "cht"), + /** 文言文 */ CHINESE_CLASSICAL("chinese.classical", "zh-CLASSICAL", baiduCode = "wyw"), + /** 粤语 */ CHINESE_CANTONESE("chinese.cantonese", "zh-CANTONESE", "yue", baiduCode = "yue"), + /** 阿尔巴尼亚语 */ ALBANIAN("albanian", "sq"), + /** 阿拉伯语 */ ARABIC("arabic", "ar", baiduCode = "ara"), + /** 阿姆哈拉语 */ AMHARIC("amharic", "am"), + /** 阿塞拜疆语 */ AZERBAIJANI("azerbaijani", "az"), + /** 爱尔兰语 */ IRISH("irish", "ga"), + /** 爱沙尼亚语 */ ESTONIAN("estonian", "et", baiduCode = "est"), + /** 巴斯克语 */ BASQUE("basque", "eu"), + /** 白俄罗斯语 */ BELARUSIAN("belarusian", "be"), + /** 保加利亚语 */ BULGARIAN("bulgarian", "bg", baiduCode = "bul"), + /** 冰岛语 */ ICELANDIC("icelandic", "is"), + /** 波兰语 */ POLISH("polish", "pl"), + /** 波斯尼亚语 */ BOSNIAN("bosnian", "bs"), + /** 波斯语 */ PERSIAN("persian", "fa"), + /** 布尔语(南非荷兰语) */ AFRIKAANS("afrikaans", "af"), + /** 丹麦语 */ DANISH("danish", "da", baiduCode = "dan"), + /** 德语 */ GERMAN("german", "de"), + /** 俄语 */ RUSSIAN("russian", "ru"), + /** 法语 */ FRENCH("french", "fr", baiduCode = "fra"), + /** 菲律宾语 */ FILIPINO("filipino", "tl"), + /** 芬兰语 */ FINNISH("finnish", "fi", baiduCode = "fin"), + /** 弗里西语 */ FRISIAN("frisian", "fy"), + /** 高棉语 */ KHMER("khmer", "km"), + /** 格鲁吉亚语 */ GEORGIAN("georgian", "ka"), + /** 古吉拉特语 */ GUJARATI("gujarati", "gu"), + /** 哈萨克语 */ KAZAKH("kazakh", "kk"), + /** 海地克里奥尔语 */ HAITIAN_CREOLE("haitianCreole", "ht"), + /** 韩语 */ KOREAN("korean", "ko", baiduCode = "kor"), + /** 豪萨语 */ HAUSA("hausa", "ha"), + /** 荷兰语 */ DUTCH("dutch", "nl"), + /** 吉尔吉斯语 */ KYRGYZ("kyrgyz", "ky"), + /** 加利西亚语 */ GALICIAN("galician", "gl"), + /** 加泰罗尼亚语 */ CATALAN("catalan", "ca"), + /** 捷克语 */ CZECH("czech", "cs"), + /** 卡纳达语 */ KANNADA("kannada", "kn"), + /** 科西嘉语 */ CORSICAN("corsican", "co"), + /** 克罗地亚语 */ CROATIAN("croatian", "hr"), + /** 库尔德语 */ KURDISH("kurdish", "ku"), + /** 拉丁语 */ LATIN("latin", "la"), + /** 拉脱维亚语 */ LATVIAN("latvian", "lv"), + /** 老挝语 */ LAO("lao", "lo"), + /** 立陶宛语 */ LITHUANIAN("lithuanian", "lt"), + /** 卢森堡语 */ LUXEMBOURGISH("luxembourgish", "lb"), + /** 罗马尼亚语 */ ROMANIAN("romanian", "ro", baiduCode = "rom"), + /** 马尔加什语 */ MALAGASY("malagasy", "mg"), + /** 马耳他语 */ MALTESE("maltese", "mt"), + /** 马拉地语 */ MARATHI("marathi", "mr"), + /** 马拉雅拉姆语 */ MALAYALAM("malayalam", "ml"), + /** 马来语 */ MALAY("malay", "ms"), + /** 马其顿语 */ MACEDONIAN("macedonian", "mk"), + /** 毛利语 */ MAORI("maori", "mi"), + /** 蒙古语 */ MONGOLIAN("mongolian", "mn"), + /** 孟加拉语 */ BENGALI("bengali", "bn"), + /** 缅甸语 */ MYANMAR("myanmar", "my"), + /** 苗语 */ HMONG("hmong", "hmn"), + /** 南非科萨语 */ XHOSA("xhosa", "xh"), + /** 南非祖鲁语 */ ZULU("zulu", "zu"), + /** 尼泊尔语 */ NEPALI("nepali", "ne"), + /** 挪威语 */ NORWEGIAN("norwegian", "no"), + /** 旁遮普语 */ PUNJABI("punjabi", "pa"), + /** 葡萄牙语 */ PORTUGUESE("portuguese", "pt"), + /** 普什图语 */ PASHTO("pashto", "ps"), + /** 齐切瓦语 */ CHICHEWA("chichewa", "ny"), + /** 日语 */ JAPANESE("japanese", "ja", baiduCode = "jp"), + /** 瑞典语 */ SWEDISH("swedish", "sv", baiduCode = "swe"), + /** 萨摩亚语 */ SAMOAN("samoan", "sm"), + /** 塞尔维亚语 */ SERBIAN("serbian", "sr"), + /** 塞索托语 */ SESOTHO("sesotho", "st"), + /** 僧伽罗语 */ SINHALA("sinhala", "si"), + /** 世界语 */ ESPERANTO("esperanto", "eo"), + /** 斯洛伐克语 */ SLOVAK("slovak", "sk"), + /** 斯洛文尼亚语 */ SLOVENIAN("slovenian", "sl", baiduCode = "slo"), + /** 斯瓦希里语 */ SWAHILI("swahili", "sw"), + /** 苏格兰盖尔语 */ SCOTS_GAELIC("scotsGaelic", "gd"), + /** 宿务语 */ CEBUANO("cebuano", "ceb"), + /** 索马里语 */ SOMALI("somali", "so"), + /** 塔吉克语 */ TAJIK("tajik", "tg"), + /** 泰卢固语 */ TELUGU("telugu", "te"), + /** 泰米尔语 */ TAMIL("tamil", "ta"), + /** 泰语 */ THAI("thai", "th"), + /** 土耳其语 */ TURKISH("turkish", "tr"), + /** 威尔士语 */ WELSH("welsh", "cy"), + /** 乌尔都语 */ URDU("urdu", "ur"), + /** 乌克兰语 */ UKRAINIAN("ukrainian", "uk"), + /** 乌兹别克语 */ UZBEK("uzbek", "uz"), + /** 西班牙语 */ SPANISH("spanish", "es", baiduCode = "spa"), + /** 希伯来语 */ HEBREW("hebrew", "iw", "he"), + /** 希腊语 */ GREEK("greek", "el"), + /** 夏威夷语 */ HAWAIIAN("hawaiian", "haw"), + /** 信德语 */ SINDHI("sindhi", "sd"), + /** 匈牙利语 */ HUNGARIAN("hungarian", "hu"), + /** 修纳语 */ SHONA("shona", "sn"), + /** 亚美尼亚语 */ ARMENIAN("armenian", "hy"), + /** 伊博语 */ IGBO("igbo", "ig"), + /** 意大利语 */ ITALIAN("italian", "it"), + /** 意第绪语 */ YIDDISH("yiddish", "yi"), + /** 印地语 */ HINDI("hindi", "hi"), + /** 印尼巽他语 */ SUNDANESE("sundanese", "su"), + /** 印尼语 */ INDONESIAN("indonesian", "id"), + /** 印尼爪哇语 */ JAVANESE("javanese", "jw"), + /** 约鲁巴语 */ YORUBA("yoruba", "yo"), + /** 越南语 */ VIETNAMESE("vietnamese", "vi", baiduCode = "vie"); diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/TranslationPane.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/TranslationPane.kt index b5aca9bb4..e1230682a 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/TranslationPane.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/TranslationPane.kt @@ -659,7 +659,6 @@ abstract class TranslationPane( .addToRight(right) } - @Suppress("InvalidBundleOrProperty") private fun getStarButtonToolTipText(favoriteId: Long?): String { return if (favoriteId == null) { message("tooltip.addToWordBook") diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/BaiduAppKeySettingsPanel.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/BaiduAppKeySettingsPanel.kt index 510d39d3b..dce952248 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/BaiduAppKeySettingsPanel.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/BaiduAppKeySettingsPanel.kt @@ -10,14 +10,13 @@ import javax.swing.Icon /** * BaiduAppKeySettingsPanel */ -class BaiduAppKeySettingsPanel(settings: Settings) - : AppKeySettingsPanel(settings.baiduTranslateSettings, BaiduTranslator) { +class BaiduAppKeySettingsPanel(settings: Settings) : + AppKeySettingsPanel(settings.baiduTranslateSettings, BaiduTranslator) { init { logo.icon = logoImage } - @Suppress("InvalidBundleOrProperty") override val name: String = message("translator.name.baidu") override fun getAppKeyLink(): String = BAIDU_FANYI_URL diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/SettingsPanel.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/SettingsPanel.kt index e4ed8d37c..e14efe93f 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/SettingsPanel.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/SettingsPanel.kt @@ -45,7 +45,6 @@ class SettingsPanel(settings: Settings, appStorage: AppStorage) : SettingsForm(s initSupport() } - @Suppress("InvalidBundleOrProperty") private fun setTitles() { translateSettingsPanel.setTitledBorder(message("settings.title.translate")) fontPanel.setTitledBorder(message("settings.title.font")) @@ -53,7 +52,6 @@ class SettingsPanel(settings: Settings, appStorage: AppStorage) : SettingsForm(s optionsPanel.setTitledBorder(message("settings.title.options")) } - @Suppress("InvalidBundleOrProperty") private fun initSelectionModeComboBox() { with(selectionModeComboBox) { model = CollectionComboBoxModel(listOf(SelectionMode.INCLUSIVE, SelectionMode.EXCLUSIVE)) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/YoudaoAppKeySettingsPanel.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/YoudaoAppKeySettingsPanel.kt index 39cf5bdf0..1859957aa 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/YoudaoAppKeySettingsPanel.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/settings/YoudaoAppKeySettingsPanel.kt @@ -10,14 +10,13 @@ import javax.swing.Icon /** * YoudaoAppKeySettingsPanel */ -class YoudaoAppKeySettingsPanel(settings: Settings) - : AppKeySettingsPanel(settings.youdaoTranslateSettings, YoudaoTranslator) { +class YoudaoAppKeySettingsPanel(settings: Settings) : + AppKeySettingsPanel(settings.youdaoTranslateSettings, YoudaoTranslator) { init { logo.icon = logoImage } - @Suppress("InvalidBundleOrProperty") override val name: String = message("translator.name.youdao") override fun getAppKeyLink(): String = YOUDAO_AI_URL diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/wordbook/WordOfTheDayDialog.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/wordbook/WordOfTheDayDialog.kt index 3b82b9743..ea5cb8d61 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/wordbook/WordOfTheDayDialog.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/ui/wordbook/WordOfTheDayDialog.kt @@ -1,5 +1,3 @@ -@file:Suppress("InvalidBundleOrProperty") - package cn.yiiguxing.plugin.translate.ui.wordbook import cn.yiiguxing.plugin.translate.message From c0ff1bff6ad72c705ffa6faa028a06ebcda5dad7 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Sun, 17 May 2020 14:40:44 +0800 Subject: [PATCH 3/6] translate text in some terminals see: #488 --- .../action/TerminalTranslationAction.kt | 24 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 5 ++++ 2 files changed, 29 insertions(+) create mode 100644 src/main/kotlin/cn/yiiguxing/plugin/translate/action/TerminalTranslationAction.kt diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TerminalTranslationAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TerminalTranslationAction.kt new file mode 100644 index 000000000..bf52f1ba0 --- /dev/null +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TerminalTranslationAction.kt @@ -0,0 +1,24 @@ +package cn.yiiguxing.plugin.translate.action + +import cn.yiiguxing.plugin.translate.util.TranslationUIManager +import cn.yiiguxing.plugin.translate.util.isNullOrBlank +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.project.DumbAware +import com.intellij.terminal.JBTerminalWidget + +class TerminalTranslationAction : AnAction(), DumbAware { + + override fun update(e: AnActionEvent) { + e.presentation.isEnabledAndVisible = !e.getSelectedTextFromTerminal().isNullOrBlank() + } + + override fun actionPerformed(e: AnActionEvent) { + val selectedText = e.getSelectedTextFromTerminal() ?: return + TranslationUIManager.showDialog(e.project).translate(selectedText) + } + + companion object { + fun AnActionEvent.getSelectedTextFromTerminal(): String? = getData(JBTerminalWidget.SELECTED_TEXT_DATA_KEY) + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 875e21626..7f75aabd2 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -170,6 +170,11 @@ class="cn.yiiguxing.plugin.translate.action.TranslateTextComponentAction" text="Translate Text Component" use-shortcut-of="$EditorTranslateAction"/> + Date: Mon, 18 May 2020 18:24:59 +0800 Subject: [PATCH 4/6] fix duplicate translation issue: #497 --- .../action/TranslateDocumentationAction.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt index 5fadfcb3c..b32eff53f 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt @@ -153,6 +153,8 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { private const val TAG_STRONG = "strong" private const val TAG_SPAN = "span" + private const val TRANSLATED_ATTR = "translated" + private val HTML_HEAD_REGEX = Regex("""<(?.+?) class="(?.+?)">""") private const val HTML_HEAD_REPLACEMENT = "<${'$'}{tag} class='${'$'}{class}'>" @@ -165,6 +167,10 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { fun getTranslatedDocumentation(documentation: String): String { val document = Jsoup.parse(documentation) + if (document.body().hasAttr(TRANSLATED_ATTR)) { + return documentation + } + val translator = TranslateService.translator val translatedDocumentation = if (translator is GoogleTranslator) { translator.getTranslatedDocumentation(document) @@ -172,10 +178,12 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { translator.getTranslatedDocumentation(document) } - return translatedDocumentation.fixHtml() + translatedDocumentation.body().attributes().add(TRANSLATED_ATTR, null) + + return translatedDocumentation.outerHtml().fixHtml() } - private fun GoogleTranslator.getTranslatedDocumentation(document: Document): String { + private fun GoogleTranslator.getTranslatedDocumentation(document: Document): Document { val body = document.body() val definition = body.selectFirst(CSS_QUERY_DEFINITION)?.apply { remove() } @@ -207,7 +215,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { } definition?.let { body.prependChild(it) } - return document.outerHtml() + return document } fun logAndShowWarning(e: Throwable, project: Project?) { @@ -223,7 +231,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { } } - private fun Translator.getTranslatedDocumentation(document: Document): String { + private fun Translator.getTranslatedDocumentation(document: Document): Document { val body = document.body() val definition = body.selectFirst(CSS_QUERY_DEFINITION)?.apply { remove() } @@ -242,7 +250,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { body.replaceWith(newBody) - return document.outerHtml() + return document } From b524a2155129c855f976424fb13398946a05cdda Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Mon, 18 May 2020 18:30:41 +0800 Subject: [PATCH 5/6] fix duplicate translation (patch 2) --- .../plugin/translate/action/TranslateDocumentationAction.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt index b32eff53f..f2d291da0 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/action/TranslateDocumentationAction.kt @@ -178,7 +178,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() { translator.getTranslatedDocumentation(document) } - translatedDocumentation.body().attributes().add(TRANSLATED_ATTR, null) + translatedDocumentation.body().attributes().put(TRANSLATED_ATTR, null) return translatedDocumentation.outerHtml().fixHtml() } From f6d157bbe27a433a268708b7e240a492914e6466 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Mon, 18 May 2020 20:23:25 +0800 Subject: [PATCH 6/6] :rocket: v2.9.1 --- CHANGELOG.md | 6 +++++- README.md | 6 +++--- gradle.properties | 2 +- src/main/resources/META-INF/plugin.xml | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bfe2a2e6..6ccb30ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,15 @@ # Change Log +## [v2.9.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.1) (2020-05-18) + +- 增强了运行控制台的取词翻译 +- 修复了一些Bug + ## [v2.9.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.0) (2020-05-16) - 增加了对 *Quick Documentation* 翻译的支持(感谢 [Nikolay Tropin](https://github.com/niktrop) ) - 单词本支持导出为txt文本(感谢 [Kaiattrib](https://github.com/kaiattrib) ) -- 修复了一些Bug ## [v2.8.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.8.1) (2020-04-07) - 修复了一些Bug diff --git a/README.md b/README.md index 5ce681b70..deb160d0e 100644 --- a/README.md +++ b/README.md @@ -172,10 +172,10 @@ FAQ 更新日志 -------- -## [v2.9.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.0) (2020-05-16) +## [v2.9.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.1) (2020-05-18) -- 增加了对 *Quick Documentation* 翻译的支持(感谢 [Nikolay Tropin](https://github.com/niktrop) ) -- 单词本支持导出为txt文本(感谢 [Kaiattrib](https://github.com/kaiattrib) ) +- 增强了运行控制台的取词翻译 +- 修复了一些Bug [完整的更新历史记录](./CHANGELOG.md) diff --git a/gradle.properties b/gradle.properties index 67f77575f..5760bef89 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # suppress inspection "UnusedProperty" for whole file -version=2.9.0 +version=2.9.1 #buildNumber= buildNumber=SNAPSHOT ideaVersion=IU-2019.3 diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 7f75aabd2..0b9435d1a 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -47,8 +47,8 @@ -
  • 增加了对 Quick Documentation 翻译的支持(感谢Nikolay Tropin
  • -
  • 单词本支持导出为txt文本(感谢Kaiattrib
  • +
  • 增强了运行控制台的取词翻译
  • +
  • 修复了一些Bug
  • Full Changelog History ]]>