Use Cloudflare Workers to reverse proxy the Google Translate API with your own domain, allowing you to use it without special network configurations.
Creating a Cloudflare Worker to Proxy the API
Here's a step-by-step guide:
Open the Cloudflare Dashboard: Go to https://dash.cloudflare.com/
Create a Worker:
After logging in, select "Workers" from the left-hand panel, and then click "Create a service" to create a new Worker.
Name Your Worker and click Save.
After saving, click "Deploy" at the bottom right
Edit the Code:
After completing the steps above, 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 results and directly returns the assembled result text
Successful code looks like
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 the Route URL:
After successful deployment, click the back button on the left, and then click "Settings" - "Triggers" in sequence.
Click "Add Custom Domain" above to bind your own domain. This is highly recommended because the
workers.dev
domain is blocked in China and cannot be used directly. By binding a custom domain, you can avoid using special network configurations.
Using in Video Translation Software
Open the settings menu in the upper left corner - Custom Translation API, fill in your API address and key (any value is fine), and then test it.
If there are no problems, select "TransAPI" in the translation channel and you can happily use the free Google Translate API.