thumbnail

最近 Google 突然关停了「谷歌翻译」以及「谷歌地图」中国地区版两大重磅产品,官方称原因是用户使用率太低。这次关停不单是网页版,依靠其服务的相关功能也会遭殃。比如 Google Chrome 谷歌浏览器目前是国内使用率最高的浏览器之一,此次停服则直接影响了浏览器内置的“自动网页翻译”功能,使得无数国内用户在浏览外语网站查阅英文资料时非常不便!

现在访问谷歌翻译主页,显示的是提示用户使用香港 Google 翻译的页面,点击页面上的链接会进入香港 Google 翻译页面。当然,如果不使用网络代理是无法使用的。这给日常需要使用谷歌翻译的小伙伴来带来了不便。如果你办公、学习都常要用到谷歌浏览器网页翻译功能。

使用Bat一键脚本

下面是获取可用IP 地址以及修改windows系统hosts 文件的自动化脚本代码,只需一键即可完成所有修改步骤。

脚本托管在 GitHub Gist 上,你也可以通过以下链接直接下载到本地使用。

使用方法

直接下载脚本文件(article013.zip)然后解压,点击解压好的“谷歌翻译修复.bat”文件

点击鼠标右键以管理员身份运行即可完成。

下载文件

下载链接

代码展示

:: Copyright (c)2022 
:: 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:

@echo off
setlocal enabledelayedexpansion
chcp 437 >NULL

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%"
set "comment=# Fix Google Translate CN"

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

if not "%old_rule%"=="null" (
    echo A rule has been added to the hosts file. 
    echo [1] Update [2] Delete
    set /p action="Enter a number to choose an action: "
    if "!action!"=="1" (
        if not "%old_rule%"=="%new_rule%" (
            echo Deleting the rule "%old_rule%"
            echo Adding the rule "%new_rule%"
            set "new_line=false"
            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%"
                if "!new_line!"=="true" >>%hosts_file% echo.
                >>%hosts_file% <NUL set /p="!rule!"
                set "new_line=true"
            )
        ) else (
            echo The rule already exists, nothing to do.
        )
    )
    if "!action!"=="2" (
        echo Deleting the rule "%old_rule%"
        set "new_line=false"
        for /f "tokens=*" %%i in ('
            type "%hosts_file%" ^| findstr /v /c:"%comment%" ^| findstr /v "%target_domain%" ^| find /v /n "" ^& break ^> "%hosts_file%"
        ') do (
            set "line=%%i"
            set "line=!line:*]=!"
            if "!new_line!"=="true" >>%hosts_file% echo.
            >>%hosts_file% <NUL set /p="!line!"
            set "new_line=true"
        )
    )
) else (
    echo Adding the rule "%new_rule%"
    echo.>>%hosts_file%
    echo %comment%>>%hosts_file%
    <NUL set /p="%new_rule%">>%hosts_file%
)

echo Done.
pause

Tips:复制代码,新建记事本文件,粘贴代码,把文件后缀名".txt"改为".bat"即可运行

结束