Skip to content

Commit

Permalink
Merge branch 'dev-2.x' into 2.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
  • Loading branch information
YiiGuxing committed May 18, 2020
2 parents 53caac8 + 923e75e commit 7c0b39b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Change Log

## [v2.9.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.1-192) (2020-05-18)

- 修复了一些Bug

## [v2.9.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.0-192) (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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TranslationPlugin
- 有道翻译
- 百度翻译
- 多语言互译
- 文档注释翻译
- 文档翻译
- 语音朗读
- 自动选词
- 自动单词拆分
Expand Down Expand Up @@ -172,10 +172,9 @@ FAQ
更新日志
--------

## [v2.9.0](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.0-192) (2020-05-16)
## [v2.9.1](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.9.1-192) (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-192
version=2.9.1-192
buildNumber=
#buildNumber=SNAPSHOT
ideaVersion=IU-2017.1
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 @@ -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 == null || text.isEmpty()) 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

0 comments on commit 7c0b39b

Please sign in to comment.