

– source – the source language (if it's not specified, Google will try to identify it automatically), The translate method has several parameters. It is accessible under the following URL: The core functionality of the Google Translate API is available through its translate method. It is important to browse through that table because you will need to include language codes stated there when submitting a request for translating a specific text. A similar table is available in the documentation.
#Php language bad code#
To do so, we will make a request to the following URL: Ĭurl_setopt($handle, CURLOPT_RETURNTRANSFER, true) //We want the result to be saved into variable, not printed outĪfter executing the code above, you should see an array of all the languages that can be processed by the Google Translate API. Translate and detect services are paid but we can use the third method – languages – just to check if our app can connect with the API. To parse it, we will use the json_decode() function. The results of each Google Translate API method are returned as a string representing a JSON object. Remember that in each call you have to pass your API key as a key parameter. The parameters passed to each method need to be URL encoded which may be achieved in PHP using the rawurlencode() function. A common way of making such a request in PHP is to use the cURL library, which we will use in the examples below. – languages, which lists the source and target languages supported by the API.Īll the methods are called via GET requests. – detect, which detects the language of the given text, – translate, which translates the given text from one language to another, You will find the key you need under Simple API access. To get the API key, just click API Access in the menu at the Google API Console page. To access the Translate API from your app, you will need an API key connected to the project you have created on the Google APIs Console. The entire configuration is available on the Google APIs Console.

If you fear getting billed too much for the translations you make, you can control the API usage in your project by setting the maximum limit of characters that can be translated daily. Naturally, if you want to translate a text into more than one target language, you will have to pay separately for each translation. Which means that translating a user comment of 300-400 characters would cost you $0.006 – $0.008. Different payment options are available in various countries but credit card payment should be recognised worldwide.Īt the time of writing this article, the usage fee was $20 per 1 million characters of translation or language detection. You will be asked to enter the payment data such as your address and credit card number. To do so, click Billing in the left menu on the Google APIs Console and then Enable billing. Since the Google Translate API is a paid service, you will also need to enable billing in your project settings. After creating a new project, just turn on Translate API on the list of all the available APIs by flicking a switch. In order to gain access to the Google Translate API, you will have to create a new project on the Google APIs Console which requires an active Google account.

#Php language bad how to#
You will learn how to gain access to the API, how to use it and how to handle errors if they occur. One service that can help is, of course, Google Translate.Īfter going through this tutorial you will be able to fetch translations from the Google Translate API right from your app. But what to do with the content posted daily by the users in comments, opinions and ratings? As this may be as valuable a part of your site as the static content, you should think of finding a way to translate it into other languages.

If your site serves visitors from different countries, you may already have translated all its static content into several languages. Note: an advanced continuation of this topic is accessible here
