Prepare a custom domain, and then use the Worker service provided by Cloudflare to reverse proxy the Google Translate API, allowing you to use the Google Translate API without needing a VPN.
Create a Worker in Cloudflare to Reverse Proxy the API
Here are the detailed steps:
Open the 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.
Name your Worker, and then click Save.
After saving, continue to click Finish in the lower right corner
Edit the 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, then click the "Deploy" button in the upper right corner to deploy.
This code parses the translation result and directly returns the assembled result text.
Successful code example:
Failed 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 the routing URL address:
After successful deployment, click the back button on the left, and then click "Settings" -- "Triggers"
Click "Add custom domain" at the top to bind your own domain. This is strongly recommended because the
workers.dev
domain is blocked in mainland China and cannot be used directly. By binding a custom domain, you can avoid using a VPN.
Using in Video Translation Software
Open the top left settings menu -- Custom Translation API, fill in your API address and key (any key will do), and then test it.
Once it works, select "TransAPI" in the translation channels to enjoy free use of the Google Translate API.