Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
  • Loading branch information
YiiGuxing committed May 18, 2020
2 parents e35ce10 + f6d157b commit 18a659d
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 49 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/cn/yiiguxing/plugin/translate/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import cn.yiiguxing.plugin.translate.util.SelectionMode
class ExclusiveTranslateAction : TranslateAction(false) {

init {
@Suppress("InvalidBundleOrProperty")
templatePresentation.description = message("action.description.exclusive")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import cn.yiiguxing.plugin.translate.util.SelectionMode
class InclusiveTranslateAction : TranslateAction(false) {

init {
@Suppress("InvalidBundleOrProperty")
templatePresentation.description = message("action.description.inclusive")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("InvalidBundleOrProperty")

package cn.yiiguxing.plugin.translate.action

import cn.yiiguxing.plugin.translate.message
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class TranslateAndReplaceAction : AutoSelectAction(true, NON_WHITESPACE_CONDITIO
}
val index = languages.indexOf(appStorage.lastReplacementTargetLanguage)

@Suppress("InvalidBundleOrProperty")
val step = object : SpeedSearchListPopupStep<Lang>(languages, title = message("title.targetLanguage")) {
override fun getTextFor(value: Lang): String = value.langName
override fun onChosen(selectedValue: Lang, finalChoice: Boolean): PopupStep<*>? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,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("""<(?<tag>.+?) class="(?<class>.+?)">""")
private const val HTML_HEAD_REPLACEMENT = "<${'$'}{tag} class='${'$'}{class}'>"

Expand All @@ -166,25 +167,31 @@ 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)
} else {
translator.getTranslatedDocumentation(document)
}

return translatedDocumentation.fixHtml()
translatedDocumentation.body().attributes().put(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() }

// 删除多余的 `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 ->
Expand All @@ -193,7 +200,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)
// 去除原文标签。
Expand All @@ -206,7 +215,7 @@ class TranslateDocumentationAction : PsiElementTranslateAction() {
}
definition?.let { body.prependChild(it) }

return document.outerHtml()
return document
}

fun logAndShowWarning(e: Throwable, project: Project?) {
Expand All @@ -222,24 +231,26 @@ 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() }

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", "<br/>"))
.let { newBody.appendChild(it) }
.addClass("content")
.append(translation.replace("\n", "<br/>"))
.let { newBody.appendChild(it) }

body.replaceWith(newBody)

return document.outerHtml()
return document
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class TranslateQuickDocAction : AnAction(), DumbAware, HintManagerImpl.ActionToI

init {
isEnabledInModalContext = true
@Suppress("InvalidBundleOrProperty")
templatePresentation.description = message("action.description.quickDoc")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class TranslateTextComponentAction : TextComponentEditorAction(Handler()), HintM

init {
isEnabledInModalContext = true
@Suppress("InvalidBundleOrProperty")
templatePresentation.description = message("action.description.textComponent")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TranslatingDocumentationProvider : DocumentationProviderEx() {
}

companion object {
private val recursion = ThreadLocal.withInitial{0}
private val recursion = ThreadLocal.withInitial { 0 }

private fun <T> nullIfRecursive(computation: () -> T?): T? {
if (recursion.get() > 0)
Expand All @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 18a659d

Please sign in to comment.