Prepare a domain, and then use the Worker service provided by Cloudflare to reverse proxy the Google Translate API, so that you can use the Google Translate API without special network configuration.
Create a Worker in Cloudflare to 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 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, 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 will look like this:
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 Address:
After successful deployment, click "Return" 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 tools.
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 will do), and then test it.
If there are no problems, select "TransAPI" in the translation channel and you can happily use the Google Translate API for free.