公司项目中涉及到国外的业务,所以每次都需要翻译文案,而且平时看源代码,里面的文档也全都是英文的,频繁的打开浏览器来翻译,着实是非常的麻烦。

还好,IDEA提供了谷歌翻译的插件(Translation),很好用,跟在浏览器上打开翻译网站翻译的效果一样:

IDEA安装了Translation谷歌翻译插件,但是提示网络异常

直接安装了一个,但是当我使用的时候却一直提示网络异常,错误信息如下:

>Plugin v2.3.2  
>IntelliJ IDEA 2019.1  
>Build #IU-191.6183.87, built on March 27, 2019  
>JRE: 1.8.0_202-release-1483-b39 x86_64  
>JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o  
>OS: macOS 10.14.4

```
cn.yiiguxing.plugin.translate.trans.TranslateException: Google Translate > 网络异常
        at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:49)
        at cn.yiiguxing.plugin.translate.trans.TranslateService$translate$inlined$executeOnPooledThread$1.run(Applications.kt:169)
        at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:311)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Connection refused (Connection refused)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:435)
        at java.net.Socket.connect(Socket.java:589)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1199)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
        at com.intellij.util.io.HttpRequests.openConnection(HttpRequests.java:615)
        at com.intellij.util.io.HttpRequests.access$300(HttpRequests.java:59)
        at com.intellij.util.io.HttpRequests$RequestImpl.getConnection(HttpRequests.java:383)
        at com.intellij.util.io.HttpRequests$RequestImpl.getInputStream(HttpRequests.java:392)
        at com.intellij.util.io.HttpRequests$RequestImpl.readString(HttpRequests.java:447)
        at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:45)
        at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:16)
        at com.intellij.util.io.HttpRequests.doProcess(HttpRequests.java:528)
        at com.intellij.util.io.HttpRequests.process(HttpRequests.java:504)
        at com.intellij.util.io.HttpRequests.access$100(HttpRequests.java:59)
        at com.intellij.util.io.HttpRequests$RequestBuilderImpl.connect(HttpRequests.java:357)
        at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:44)
        ... 7 more
```

第一反应是本机是不是断网了,查看之后发现网络并没有问题,可以访问谷歌翻译的网站,而且访问数据库之类的也都是正常的。

紧接着又怀疑是不是因为没翻墙的缘故,因为之前访问谷歌翻译都是使用的http://translate.google.cn,而不是使用的.com,可是检查了之后发现也没问题,不管是.cn还是.com,本机都是可以访问的。

无奈之下重启IDEA,竟然还是不行,打开之后依旧说网络异常,真的是让我非常生气,甚至都想要卸载这个破插件了。

抱着最后一丝希望,憋了个大招:重启电脑,没想到问题竟然解决了,重启之后再次打开IDEA就正常了,就算我将VPN关了也依旧还是可以使用插件进行翻译。

什么也没做,问题就这么好了,莫名其妙,真的是奇了个怪!!!

2019.7.11日更新:

上次碰见翻译插件异常的问题,虽然没找到直接的原因,但是重启机器之后莫名其妙好了,今天竟然又不行了。。。

>Plugin v2.3.2  
>IntelliJ IDEA 2019.1.3  
>Build #IU-191.7479.19, built on May 28, 2019  
>JRE: 1.8.0_202-release-1483-b58 x86_64  
>JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o  
>OS: macOS 10.14.5

```
cn.yiiguxing.plugin.translate.trans.TranslateException: Google Translate > 翻译失败: 未知错误
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:49)
    at cn.yiiguxing.plugin.translate.trans.TranslateService$translate$inlined$executeOnPooledThread$1.run(Applications.kt:169)
    at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:311)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:994)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
    at com.intellij.util.io.HttpRequests.openConnection(HttpRequests.java:615)
    at com.intellij.util.io.HttpRequests.access$300(HttpRequests.java:59)
    at com.intellij.util.io.HttpRequests$RequestImpl.getConnection(HttpRequests.java:383)
    at com.intellij.util.io.HttpRequests$RequestImpl.getInputStream(HttpRequests.java:392)
    at com.intellij.util.io.HttpRequests$RequestImpl.readString(HttpRequests.java:447)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:45)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:16)
    at com.intellij.util.io.HttpRequests.doProcess(HttpRequests.java:528)
    at com.intellij.util.io.HttpRequests.process(HttpRequests.java:504)
    at com.intellij.util.io.HttpRequests.access$100(HttpRequests.java:59)
    at com.intellij.util.io.HttpRequests$RequestBuilderImpl.connect(HttpRequests.java:357)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:44)
    ... 7 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:505)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
    ... 28 more
```

对比两次错误信息,上次是连接被拒绝(Connection refused),这次是HTTP握手失败(Remote host closed connection during handshake),痛定思痛,将插件开发者提供的文档(http://yiiguxing.github.io/TranslationPlugin/start.html)阅读了一遍,发现人家早就已经总结过这类似的问题了,可惜之前眼瞎一直没看到。

FAQ:出现网络错误或者网络连接超时怎么办?

答:

检查网络环境,确保网络畅通;

检查是否是因为使用了代理软件而导致插件无法访问翻译API;

检查IDE代理配置,查看是否是因为IDE代理配置导致的问题;

如果使用的是谷歌翻译,检查谷歌翻译配置,查看是否勾选了`使用translate.google.com`选项,translate.google.com在国内是需要VPN才能访问的。另外,TTS功能使用的也是谷歌翻译的API;

瞬间反应过来,我之前用的代理过期了,但是idea里面的代理却没有移除,如下图:

IDEA安装了Translation谷歌翻译插件,但是提示网络异常

将代理移除之后,本机使用正常!

最后再提一句,看看有没有人和我一样的疑惑:“我该如何将插件配置的谷歌翻译地址从com改为cn?”

没看文档之前(截止到今天),我也一直有这个疑问,但是百度谷歌找了一圈都没有答案,后来也就不了了之了,但是今天在看插件github上issue的时候,我发现作者已经解答过这个问题了(https://github.com/YiiGuxing/TranslationPlugin/issues/213):

国内用户默认是translate.google.cn的,除非你设备的语言环境为非中文。

2019.7.22日更新:

今天又出现了翻译不了的问题,提示403,看起来像是被谷歌翻译给禁了IP一样,但是直接在浏览器上访问谷歌翻译是没问题的,而且还是非必现,偶尔才会出现,更神奇的是,错误就算出现了,也能翻译成功,希望插件作者会尽快搞定这个问题。

>Plugin v2.3.2  
>IntelliJ IDEA 2019.1.3  
>Build #IU-191.7479.19, built on May 28, 2019  
>JRE: 1.8.0_202-release-1483-b58 x86_64  
>JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o  
>OS: macOS 10.14.5

```
cn.yiiguxing.plugin.translate.trans.TranslateException: Google Translate > 翻译失败: 未知错误
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:49)
    at cn.yiiguxing.plugin.translate.trans.TranslateService$translate$inlined$executeOnPooledThread$1.run(Applications.kt:169)
    at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:311)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.intellij.util.io.HttpRequests$HttpStatusException: Request failed with status code 403. Status=403, Url=https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dt=rm&dj=1&ie=UTF-8&oe=UTF-8&sl=auto&tl=en&hl=zh-CN&tk=361788.206317&q=fan%27yi
    at com.intellij.util.io.HttpRequests.throwHttpStatusError(HttpRequests.java:649)
    at com.intellij.util.io.HttpRequests.openConnection(HttpRequests.java:630)
    at com.intellij.util.io.HttpRequests.access$300(HttpRequests.java:59)
    at com.intellij.util.io.HttpRequests$RequestImpl.getConnection(HttpRequests.java:383)
    at com.intellij.util.io.HttpRequests$RequestImpl.getInputStream(HttpRequests.java:392)
    at com.intellij.util.io.HttpRequests$RequestImpl.readString(HttpRequests.java:447)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:45)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator$translate$2.process(AbstractTranslator.kt:16)
    at com.intellij.util.io.HttpRequests.doProcess(HttpRequests.java:528)
    at com.intellij.util.io.HttpRequests.process(HttpRequests.java:504)
    at com.intellij.util.io.HttpRequests.access$100(HttpRequests.java:59)
    at com.intellij.util.io.HttpRequests$RequestBuilderImpl.connect(HttpRequests.java:357)
    at cn.yiiguxing.plugin.translate.trans.AbstractTranslator.translate(AbstractTranslator.kt:44)
    ... 7 more
```

看完文章,有任何疑问,请加入群聊一起交流!!!

很赞哦! ()