If you have your own domain name, and then use the Worker service provided by Cloudflare to reverse proxy the Google Translate API, you can use the Google Translate API without scientific internet access.
Create a Worker in Cloudflare to Reverse Proxy the API
The following are the detailed steps:
Open Cloudflare Console: Visit https://dash.cloudflare.com/
Create a Worker:
After logging in, select "Workers" in the left panel, and then click "Create" to create a new Worker service.
Give your Worker a name, and then click Save.
After saving, continue to click Finish in the lower right corner
Edit code:
After completing the above steps, click "Edit Code" in the upper right corner to enter the code editing page. Delete the default code and replace it with the following code, and then click the "Deploy" button in the upper right corner to deploy.
This code has parsed the translation results and will directly return the assembled result text
Successful code is similar to
Failure result:
export default {
async fetch(request, env, ctx) {
let url = new URL(request.url);
if(url.pathname.startsWith('/')){
url.hostname="translate.googleapis.com";
let new_request = new Request(url, request)
let response=await fetch(new_request)
if(response.status!==200){
return new Response(JSON.stringify({code:1,msg:response.text}), {
status: 200,
headers: {
'content-type': 'application/json',
},
});
}
let jsonData = await response.json();
let str=jsonData[0].map(it=>{
return it[0]
})
let data={code:0,msg:"ok",text:str.join('')}
return new Response(JSON.stringify(data), {
status: 200,
headers: {
'content-type': 'application/json',
},
});
}
return await env.ASSETS.fetch(request);
},
};
Get route URL address:
After the deployment is successful, click Return on the left, and then click "Settings" - "Triggers" in turn
Click "Add Custom Domain" above to bind your own domain name. It is strongly recommended to do so, because the
workers.dev
domain name has been blocked in China and cannot be used directly. By binding a custom domain name, you can avoid using scientific internet access tools.
Use in Video Translation Software
Open the upper left corner Settings menu - Custom Translation API, fill in your api address and key (any can be), and then test it
After there is no problem, select "TransAPI" in the translation channel and you can happily use the Google translation api for free.