Google 翻译

笪涛
2023-12-01
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;


public class QuickstartSample {

    static{
        //国内访问需要加上代理
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyPort", "1080");
    }
    public static void main(String... args) throws Exception {

        //服务账号json  配置的环境变量 为空则未成功配置
        System.out.println( System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));

        // Instantiates a client
        Translate translate = TranslateOptions.getDefaultInstance().getService();

        TranslateOptions.getDefaultApiKey();
        // The text to translate
        String text = "Hello, world!";

        // Translates some text into Russian
        Translation translation =
                translate.translate(
                        text,
                        Translate.TranslateOption.sourceLanguage("en"),
                        Translate.TranslateOption.targetLanguage("zh-CN"));


        System.out.printf("Text: %s%n", text);
        System.out.printf("Translation: %s%n", translation.getTranslatedText());
    }
}
 类似资料: