C#开发JRPG/视觉小说文本翻译工具:谷歌URL调用遭封禁求助
Hey there, I’ve run into exactly this kind of temporary ban issue when scraping public translation endpoints before—those web-facing interfaces are explicitly designed to block automated requests. Let’s walk through practical, C#-friendly solutions to get your project back on track:
1. Switch to the Official Google Cloud Translation API
The public web interface you’re hitting isn’t meant for programmatic use. Google’s official API is rate-limited but legitimate, and comes with a free tier that should cover early-stage testing. Here’s how to integrate it in C#:
- First, install the NuGet package:
Install-Package Google.Cloud.Translation.V2 - Authenticate with a service account key (you’ll need to create one in the Google Cloud Console)
- Basic usage example:
using Google.Cloud.Translation.V2; var client = TranslationClient.FromCredentialFile("path/to/your-service-account-key.json"); var response = await client.TranslateTextAsync( text: ocrExtractedText, targetLanguage: "en", sourceLanguage: "ja"); string translatedText = response.TranslatedText;
2. Add Request Throttling & Retry Logic
Even with the official API, you can hit quota limits. Use a library like Polly to implement exponential backoff and retries, which helps avoid triggering bans:
- Install Polly:
Install-Package Polly - Example retry policy:
using Polly; var retryPolicy = Policy .Handle<GoogleApiException>(ex => ex.HttpStatusCode == System.Net.HttpStatusCode.TooManyRequests) .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); string translatedText = await retryPolicy.ExecuteAsync(async () => { var response = await client.TranslateTextAsync(ocrExtractedText, "en", "ja"); return response.TranslatedText; });
3. Use Alternative Translation APIs
If Google’s quota is too restrictive, consider these alternatives with more generous free tiers:
- DeepL API: Offers high-quality translation, with a free tier for up to 500k characters/month. Their .NET client is easy to use:
Install-Package DeepL - Microsoft Translator API: Part of Azure Cognitive Services, has a free tier and solid C# SDK support.
4. Go Offline with Local Translation Models
For complete independence from cloud APIs (and no risk of bans), use open-source local translation models. You can integrate them via ONNX Runtime in C#:
- Load a pre-trained Japanese-to-English translation model (e.g., converted to ONNX from Hugging Face)
- Use
Microsoft.ML.OnnxRuntimeNuGet package to run inference locally. This is also perfect for offline play scenarios.
5. Optimize OCR to Reduce API Calls
Cut down on unnecessary requests in the first place:
- Cache translated text: Store already-translated Japanese strings in a dictionary or local database, so you don’t re-translate identical text boxes
- Only detect changes: Compare OCR results to the last frame’s text—only send a translation request if the content has actually changed
These steps should eliminate the temporary ban issue while making your tool more robust overall. Pick the solution that fits your project’s stage (official API is easiest for early development, local models are better for long-term offline support).
内容的提问来源于stack exchange,提问作者DesMondesBlut

