Google翻译API突然不能用,其原因是Google重定向了中国的Google翻译网页版,直接把API也重定向了,所以我们通过修改hosts文件来实现谷歌翻译复活。

hosts路径

Windows:C:\Windows\System32\drivers\etc\hosts
Mac:/etc/hosts
Linux:/etc/hosts
Android:/system/etc/hosts
iOS:/etc/hosts

找谷歌国内IP

通过ping的方法,按win键+R,然后输入ping google.cn -t,按确定。
然后就能ping出你目前连得到的谷歌服务器IP地址,我的这里是203.208.40.66,记住这个IP地址。
上面是获取谷歌IP地址最直接的方法,当然你也可以打开下面的网址:https://ping.chinaz.com/translate.google.cn,自己找找离自己近一点的,比如我在广东,找广州的IP地址也可以。

修改Hosts

上面找到了hotst所在的地址,用记事本打开hosts文件,把“地址+空格+translate.googleapis.com”输入在最后面。例如我输入的是:203.208.40.98 translate.googleapis.com

温馨提示:hosts文件如果是设置为只读了,可能会修改不了,大家右键-属性,然后把“只读”去掉勾即可。

最后保存,保存后再用谷歌即可正常翻译了.

一键化脚本

保存为 bat, 管理员运行

:: Copyright (c)2022 https://bookfere.com
:: This is a batch script for fixing Google Translate and making it available
:: in the Chinese mainland. If you experience any problem, visit the page below:
:: https://bookfere.com/post/1020.html

@setlocal enabledelayedexpansion
@echo off

set "source_domain=google.cn"
set "target_domain=translate.googleapis.com"

set "hosts_file=C:\Windows\System32\drivers\etc\hosts"

for /f "skip=4 tokens=2" %%a in ('"nslookup %source_domain% 2>NUL"') do set ip=%%a
set "old_rule=null"
set "new_rule=%ip% %target_domain%"

for /f "tokens=*" %%i in ('type %hosts_file%') do (
    set "line=%%i"
    :: Retrieve the rule If the target domain has been exists in the line.
    if not "!line:%target_domain%=!"=="%%i" set "old_rule=%%i"
)

if not "%old_rule%"=="null" (
    if not "%old_rule%"=="%new_rule%" (
        echo Deleting the rule "%old_rule%"
        echo Adding the rule "%new_rule%"
        for /f "tokens=*" %%i in ('type "%hosts_file%" ^| find /v /n "" ^& break ^> "%hosts_file%"') do (
            set "rule=%%i"
            set "rule=!rule:*]=!"
            if "%old_rule%"=="!rule!" set "rule=%new_rule%"
            >>%hosts_file% echo(!rule!
        )
    ) else (
        echo The rule already exists, nothing to do.
    )
) else (
    echo Adding the rule "%new_rule%"
    echo.>>%hosts_file%
    echo.>>%hosts_file%
    echo # Fix Google Translate CN>>%hosts_file%
    echo %new_rule%>>%hosts_file%
)

echo Done.
pause

New

如果以上失效 参考 https://github.com/Ponderfly/GoogleTranslateIpCheck