How can I use http://translate.google.com/ to translate the string in Java program?

前端 未结 4 1734
庸人自扰
庸人自扰 2021-01-29 01:18

I want to use http://translate.google.com/ to translate the string. And now I want to sent a string from a java program in http://translate.google.com/ to translate the string f

相关标签:
4条回答
  • 2021-01-29 02:05

    The wrong way to do that : use an HTTPClient to emulate, in Java, a browser request. It's a bad way of using a website, as you'll make dirty things in HTTP, and your program will have to be modified each time Google modify the HTML pages on translate.google.com (even if that should be pretty rare).

    The right way of doing that : use the Google Translate API provided by Google for that purpose. It's just a REST service so it works quite easily in JAVA.

    Beware that the number of translations that you can do each day is, as far as i remember, limited to a certain amount each day (check the online conditions on the API website). At first glance after just checking that, it seems that the v2 API is not free anymore, i don't know if you can stick to the v1 one.

    0 讨论(0)
  • 2021-01-29 02:09

    I used this code on my button for translate:

    String translate = "translate this string";    
    String locale = Locale.getDefault().getLanguage();
    Uri uri = Uri.parse("https://translate.google.com/#auto/"+ locale + "/" + translate);         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            getApplicationContext.startActivity(intent);
    

    I used #auto to detect automatic from string translate, and locale to detect locale language from phone.

    Hope this helps :)

    0 讨论(0)
  • 2021-01-29 02:10

    If google is not a must you can consider Bing translator. Here is a link on how to use the free APIs as well (the example uses C# but you can easily write the same in JAVA). We are using this in our project and it works quite well.

    0 讨论(0)
  • 2021-01-29 02:16

    Easy task.

    Use this - http://translate.google.com/#{fromLanguage}|{toLanguage}|{your_string_here}

    Just replace languages with yours (you can check it in translator - short names) And add your string that you want to translate.

    You can make request to this site

    0 讨论(0)
提交回复
热议问题