You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何使用Google翻译API?导入示例代码遇编译异常求助

Fixing Your Google Translate API Compilation Errors

Hey there, let's break down exactly what's going wrong here and get your translation tool back on track.

Why You're Seeing These Errors

First off, the com.google.api.translate classes you're trying to use are from Google's old, long-deprecated Translate API—Google shut down this service years ago. That's why your IDE can't find the classes (hence the red squiggles) and the import statements show up as "unused"—even if you could resolve the dependency, there's no working service behind those classes anymore.

Step-by-Step Solutions

Option 1: Switch to Google Cloud Translate API (Official, Modern Alternative)

This is Google's current supported translation service (it's paid but has a free tier for small-scale use):

  • Head to the Google Cloud Console, enable the Cloud Translate API, and create an API key or service account.
  • Add the dependency to your project:
    For Gradle (Android/Java):
    dependencies {
        implementation 'com.google.cloud:google-cloud-translate:2.31.0' // Use the latest available version
    }
    
  • Replace your old code with the modern API usage example:
    import com.google.cloud.translate.Translate;
    import com.google.cloud.translate.TranslateOptions;
    import com.google.cloud.translate.Translation;
    
    public class MyTranslator {
        public String translateText(String input, String targetLang) {
            // Initialize the client with your API key
            Translate translate = TranslateOptions.newBuilder()
                .setApiKey("YOUR_GOOGLE_CLOUD_API_KEY")
                .build()
                .getService();
            
            Translation translation = translate.translate(
                input,
                Translate.TranslateOption.targetLanguage(targetLang)
            );
            
            return translation.getTranslatedText();
        }
    }
    

Option 2: Use a Free, Open-Source Alternative (LibreTranslate)

If you don't want to use a paid service, LibreTranslate is a great open-source option (you can use community-run public instances or host your own):

  • Add the client dependency to your project:
    For Gradle:
    dependencies {
        implementation 'com.github.libretranslate:libretranslate-client:1.3.1'
    }
    
  • Example code to use it:
    import com.github.libretranslate.LibreTranslate;
    import com.github.libretranslate.TranslationResponse;
    
    public class LibreTranslator {
        public String translate(String input, String sourceLang, String targetLang) throws Exception {
            // Initialize with a public LibreTranslate instance (or your self-hosted server URL)
            LibreTranslate lt = new LibreTranslate("https://libretranslate.de");
            
            TranslationResponse response = lt.translate(input, sourceLang, targetLang);
            return response.getTranslatedText();
        }
    }
    

Note on "Unused Import" Warnings

Those warnings are just a side effect of the missing classes—once you replace the old imports with working API classes and actually use them in your code (like the examples above), the IDE will stop flagging them as unused.

内容的提问来源于stack exchange,提问作者Hatay Berkay Işıkoğlu

火山引擎 最新活动