How to use Google Translate API in my Java application?

后端 未结 5 1024
攒了一身酷
攒了一身酷 2020-11-27 12:58

If I pass a string (either in English or Arabic) as an input to the Google Translate API, it should translate it into the corresponding other language and give the translate

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 13:24

    I’m tired of looking for free translators and the best option for me was Selenium (more precisely selenide and webdrivermanager) and https://translate.google.com

    import io.github.bonigarcia.wdm.ChromeDriverManager;
    import com.codeborne.selenide.Configuration;
    import io.github.bonigarcia.wdm.DriverManagerType;
    import static com.codeborne.selenide.Selenide.*;
    
    public class Main {
    
        public static void main(String[] args) throws IOException, ParseException {
    
            ChromeDriverManager.getInstance(DriverManagerType.CHROME).version("76.0.3809.126").setup();
            Configuration.startMaximized = true;
            open("https://translate.google.com/?hl=ru#view=home&op=translate&sl=en&tl=ru");
            String[] strings = /some strings to translate
            for (String data: strings) {
                $x("//textarea[@id='source']").clear();
                $x("//textarea[@id='source']").sendKeys(data);
                String translation = $x("//span[@class='tlid-translation translation']").getText();
            }
        }
    }
    

提交回复
热议问题