Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Jul 22, 2019
2 parents a33cb11 + 0668ea3 commit 3c800ba
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [v2.3.7](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.3.7) (2019-07-22)

- 优化交互检验
- 修复对话框上不能使用翻译替换的问题

## [v2.3.6](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.3.6) (2019-07-15)

- 修复Google翻译Forbidden的问题
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ FAQ

更新日志
--------
## [v2.3.6](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.3.6) (2019-07-15)
## [v2.3.7](https://github.com/YiiGuxing/TranslationPlugin/tree/v2.3.7) (2019-07-22)

- 修复Google翻译Forbidden的问题
- 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.3.6
version=2.3.7
buildNumber=
ideaVersion=2017.1
javaVersion=1.8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.yiiguxing.plugin.translate

import cn.yiiguxing.plugin.translate.trans.TKK
import cn.yiiguxing.plugin.translate.util.TranslateService
import cn.yiiguxing.plugin.translate.util.TranslationUIManager
import com.intellij.openapi.components.ApplicationComponent
Expand All @@ -15,7 +14,6 @@ class TranslationAppComponent : ApplicationComponent {
override fun getComponentName(): String = javaClass.name

override fun initComponent() {
TKK.updateAsync()
// initialize translate service.
TranslateService.install()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.yiiguxing.plugin.translate

import cn.yiiguxing.plugin.translate.trans.BaiduTranslator
import cn.yiiguxing.plugin.translate.trans.TKK
import cn.yiiguxing.plugin.translate.trans.YoudaoTranslator
import cn.yiiguxing.plugin.translate.util.App
import cn.yiiguxing.plugin.translate.util.Settings
Expand All @@ -18,7 +17,6 @@ import javax.swing.event.HyperlinkEvent
class TranslationProjectComponent(project: Project) : AbstractProjectComponent(project) {

override fun projectOpened() {
TKK.updateAsync()
checkConfig()
checkUpdate()
TranslationUIManager.installStatusWidget(myProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class TranslateAndReplaceAction : AutoSelectAction(true, NON_WHITESPACE_CONDITIO

fun Editor.canShowPopup(selectionRange: TextRange, targetText: String): Boolean {
return !isDisposed &&
selectionRange.endOffset < document.textLength &&
selectionRange.endOffset <= document.textLength &&
targetText == document.getText(selectionRange) &&
selectionRange.containsOffset(caretModel.offset)
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/kotlin/cn/yiiguxing/plugin/translate/trans/TK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package cn.yiiguxing.plugin.translate.trans

import cn.yiiguxing.plugin.translate.DEFAULT_USER_AGENT
import cn.yiiguxing.plugin.translate.util.*
import cn.yiiguxing.plugin.translate.util.Notifications
import cn.yiiguxing.plugin.translate.util.Settings
import cn.yiiguxing.plugin.translate.util.i
import cn.yiiguxing.plugin.translate.util.w
import com.intellij.openapi.diagnostic.Logger
import com.intellij.util.io.HttpRequests
import java.lang.Math.abs
Expand All @@ -32,6 +35,7 @@ object TKK {
private const val MIM = 60 * 60 * 1000
private const val ELEMENT_URL = "https://translate.google.com/translate_a/element.js"
private const val ELEMENT_URL_CN = "https://translate.google.cn/translate_a/element.js"
private const val NOTIFICATION_DISPLAY_ID = "TKK Update Failed"

private val logger: Logger = Logger.getInstance(GoogleTranslator::class.java)

Expand All @@ -41,17 +45,21 @@ object TKK {

private var innerValue: Pair<Long, Long> = 0L to 0L

private var needUpdate: Boolean = true

val value get() = update()

@Synchronized
fun update(): Pair<Long, Long> {
val now = System.currentTimeMillis() / MIM
val (curVal) = innerValue
if (now == curVal) {
if (!needUpdate && now == curVal) {
return innerValue
}

innerValue = updateFromGoogle() ?: now to (abs(generator.nextInt().toLong()) + generator.nextInt().toLong())
val newTKK = updateFromGoogle()
needUpdate = newTKK == null
innerValue = newTKK ?: now to (abs(generator.nextInt().toLong()) + generator.nextInt().toLong())
return innerValue
}

Expand All @@ -76,19 +84,20 @@ object TKK {

value1 to value2
} else {
logger.w("Update TKK failed: TKK not found.")
logger.w("TKK update failed: TKK not found.")
Notifications.showWarningNotification(
NOTIFICATION_DISPLAY_ID,
"TKK",
"TKK update failed: TKK not found."
)

null
}
} catch (e: Throwable) {
logger.e("Update TKK failed", e)
null
}
}
logger.w("TKK update failed", e)
Notifications.showErrorNotification(null, NOTIFICATION_DISPLAY_ID, "TKK", "Failed to update TKK.", e)

fun updateAsync() {
executeOnPooledThread {
update()
null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ fun Notification.show(project: Project? = null) {
/**
* Copy the stack trace to clipboard.
*/
fun Throwable.copyToClipboard() {
fun Throwable.copyToClipboard(message: String? = null) {
val stringWriter = StringWriter()
PrintWriter(stringWriter).use {
if (!message.isNullOrBlank()) {
it.println(message)
it.println()
}

it.println(App.systemInfo.split("\n").joinToString(separator = " \n>", prefix = ">"))
it.println()

Expand Down
58 changes: 39 additions & 19 deletions src/main/kotlin/cn/yiiguxing/plugin/translate/util/Notifications.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package cn.yiiguxing.plugin.translate.util

import cn.yiiguxing.plugin.translate.HTML_DESCRIPTION_SETTINGS
Expand All @@ -9,26 +11,44 @@ import javax.swing.event.HyperlinkEvent

object Notifications {

fun showErrorNotification(project: Project?,
displayId: String,
title: String,
message: String,
throwable: Throwable) {
fun showErrorNotification(
project: Project?,
displayId: String,
title: String,
message: String,
throwable: Throwable
) {
NotificationGroup(displayId, NotificationDisplayType.TOOL_WINDOW, true)
.createNotification(
title,
"""$message (<a href="$HTML_DESC_COPY_TO_CLIPBOARD">Copy to Clipboard</a>)""",
NotificationType.WARNING,
object : NotificationListener.Adapter() {
override fun hyperlinkActivated(notification: Notification, event: HyperlinkEvent) {
notification.expire()
when (event.description) {
HTML_DESCRIPTION_SETTINGS -> OptionsConfigurable.showSettingsDialog(project)
HTML_DESC_COPY_TO_CLIPBOARD -> throwable.copyToClipboard(message)
}
}
})
.show(project)
}

fun showNotification(
displayId: String,
title: String,
message: String,
type: NotificationType,
project: Project? = null
) {
NotificationGroup(displayId, NotificationDisplayType.TOOL_WINDOW, true)
.createNotification(
title,
"""$message (<a href="$HTML_DESC_COPY_TO_CLIPBOARD">Copy to Clipboard</a>)""",
NotificationType.WARNING,
object : NotificationListener.Adapter() {
override fun hyperlinkActivated(notification: Notification, event: HyperlinkEvent) {
notification.expire()
when (event.description) {
HTML_DESCRIPTION_SETTINGS -> OptionsConfigurable.showSettingsDialog(project)
HTML_DESC_COPY_TO_CLIPBOARD -> throwable.copyToClipboard()
}
}
})
.show(project)
.createNotification(title, message, type, null)
.show(project)
}

fun showWarningNotification(displayId: String, title: String, message: String, project: Project? = null) {
showNotification(displayId, title, message, NotificationType.WARNING, project)
}

}
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

<change-notes><![CDATA[
<ul>
<li>修复Google翻译Forbidden的问题</li>
<li>BUG修复</li>
<li>优化交互检验</li>
<li>修复对话框上不能使用翻译替换的问题</li>
</ul>
<a href="https://github.com/YiiGuxing/TranslationPlugin/blob/master/CHANGELOG.md"><b>Full Changelog History</b></a>
]]></change-notes>
Expand Down

0 comments on commit 3c800ba

Please sign in to comment.