Provides a Simple Java API Client for using Google Translate.
Generates strings.xml file in correct folder (SPANISH => values-es)
Uses Google Cloud Translation API
Small library size - less than 50Kb.
git clone https://github.com/the-mac/google-api-translate-java.git
- Visit Google APIs & Services to create key
- Click on Create if there is no existing project
- Complete project Creation
- Click on Dashboard
- Click on ENABLE APIS AND SERVICES
- Type translate
- Click on Google Cloud Translation API
- Click on Enable (Note: You will need to set up Billing / For Free Trial)
- Click on Credentials
- Click on Create Credentials
- Click on API Key
- Copy/Paste sample-project.properties to a project.properties file
apiKey=API_KEY_HERE
fromLanguage=SPANISH
toLanguage=ENGLISH
- Copy Your API Key value from Google Console
- Paste your New API Key replacing API_KEY_HERE
apiKey=AIzaSyAkYbwMzqgUOo_nXHe7n_XIoLEZG8Su6N4
fromLanguage=SPANISH
toLanguage=ENGLISH
- Click Close on Google Console API Key Prompt
- Run Main java application
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class Main {
private static Properties properties = new Properties();
public static void main(String[] args) throws Exception {
InputStream input = null;
String translatedText = null;
try {
input = new FileInputStream("project.properties");
properties.load(input);
// Set the Google Translate API key
GoogleAPI.setKey(properties.getProperty("apiKey"));
String text = "Bonjour le monde";
Language from = Language.FRENCH;
Language to = Language.ENGLISH;
System.out.printf("Translating: %s \n\nFrom %s to %s\n\n", text, from, to);
System.out.println(Translate.DEFAULT.execute(text, from, to)+"\n");
System.out.println("Completed Translate API call successfully");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The result should look like the following:
Translating: Bonjour le monde
From fr to en
Hi world
Completed Translate API call successfully