
Automatic language detection is a technology for determining the language of texts. It is a great use case for artificial intelligence. The detection model can analyze the given text and identify the language. In this way, it can help you to sort information quickly and apply additional layers of language-specific workflows effectively. For example, you can apply the subtitles to a video in the right language. In this post, you will find the details of automating language detection with artificial intelligence by utilizing Languagelayer. This API could also be combined with a geolocation API. Now, let’s dive in.

Table of Contents
What is Languagelayer?
Languagelayer is a powerful REST API for automating language detection. It is lightning fast. Thanks to the support for lightweight JSON format. Also, it supports 173 languages. So, you can use it to identify a variety of languages and accents. Besides, the detection algorithm of Languagelayer is powered by AI. So, it improves continuously with each language detection API request performed. Overall, it is one of the best REST APIs for automating language detection that you can find online.
Why should I use Languagelayer?
- Supports 173 languages
- Powered by AI that improves continuously
- Supports 256-bit HTTPS for secure and encrypted datastreams
- Consumes significantly low bandwidth
- Provides world-class tech support
How can I automate language detection with Languagelayer?
Languagelayer supports a variety of programming languages. But in this post, you will learn the way of automating language detection in two languages: PHP and JavaScript.
How to Automate Standard Language Detection in PHP
1. First, you need to find the Languagelayer access key. You can get it for free from here.
2. Now, you have to set the access key in PHP with this code:
1 |
$access_key = 'YOUR_ACCESS_KEY'; |
3. Next, you have to set the query text.
1 |
$query = 'Hello my friend, how are you today?'; |
4. Then you have to initialize CURL.
1 2 |
$ch = curl_init('http://api.languagelayer.com/detect?access_key='.$access_key.'&query='.$query.''); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
5. Now, you have to store the data.
1 2 |
$json = curl_exec($ch); curl_close($ch); |
6. Finally, it’s time for decoding the JSON response.
1 |
$detection_result = json_decode($json, true); |
The overall code will look like this:
// set API Access Key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// set API Access Key $access_key = 'YOUR_ACCESS_KEY'; // set query text $query = 'Hello my friend, how are you today?'; // Initialize CURL: $ch = curl_init('http://api.languagelayer.com/detect?access_key='.$access_key.'&query='.$query.''); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Store the data: $json = curl_exec($ch); curl_close($ch); // Decode JSON response and store array: $detection_result = json_decode($json, true); |
How to Automate Batch Language Detection in PHP
1. First, you have to define a function, called languagelayer_batch(). It will have a parameter, called $query_array. Then you have to set the access key.
1 2 |
function languagelayer_batch($query_array) { $access_key = 'YOUR_ACCESS_KEY'; |
2. Next, you have to create a foreach loop.
1 |
foreach($query_array as $value) { $fields_string .= 'query[]='.$value.'&'; } |
3. Then you have to trim “&” from $fields_string.
1 |
rtrim($fields_string, '&'); |
4. Now, you have to initialize CURL.
1 2 3 4 5 |
$ch = curl_init('http://api.languagelayer.com/batch?access_key='.$access_key); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
5. Next, you have to store the data.
1 2 |
$json = curl_exec($ch); curl_close($ch); |
6. Now, you can view the JSON response.
1 |
echo $json; |
7. Then you have to decode the JSON response.
1 |
$detection_results = json_decode($json, true); |
8. Now, you can access and use your preferred validation result objects,
1 2 3 |
return $detection_results; } |
9. Then you have to set query texts.
1 |
$query_array = array('hey friend', 'hola amigo', 'hallo freund'); |
10. Finally, you have to store results array.
1 |
$results_array = languagelayer_batch($query_array); |
Overall, the code will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function languagelayer_batch($query_array) { // set access key $access_key = 'YOUR_ACCESS_KEY'; foreach($query_array as $value) { $fields_string .= 'query[]='.$value.'&'; } rtrim($fields_string, '&'); // Initialize CURL: $ch = curl_init('http://api.languagelayer.com/batch?access_key='.$access_key); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Store the data: $json = curl_exec($ch); curl_close($ch); echo $json; // Decode JSON response: $detection_results = json_decode($json, true); // Access and use your preferred validation result objects return $detection_results; } // set query texts $query_array = array('hey friend', 'hola amigo', 'hallo freund'); // store results array $results_array = languagelayer_batch($query_array); |
How to Automate Standard Language Detection Using JavaScript
1. First, you have to set the access key and query.
1 2 |
var access_key = 'YOUR_ACCESS_KEY'; var query = 'Hello my friend, how are you?'; |
2. Now, you have to make an AJAX call and access your preferred validation results object.
1 2 3 4 5 6 7 8 9 |
$.ajax({ url: 'http://api.languagelayer.com/detect?access_key=' + access_key + '&query=' + encodeURIComponent(query), dataType: 'jsonp', success: function(json) { console.log(json.success); } }); |
Overall, the code will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// set endpoint and your access key var access_key = 'YOUR_ACCESS_KEY'; var query = 'Hello my friend, how are you?'; // AJAX call $.ajax({ url: 'http://api.languagelayer.com/detect?access_key=' + access_key + '&query=' + encodeURIComponent(query), dataType: 'jsonp', success: function(json) { // Access and use your preferred validation result objects console.log(json.success); } }); |
How can I get started with automating language detection with Languagelayer?
That’s how you use the Languagelayer API to automate language detection. Now, you can implement the specific feature on your PHP or JavaScript web application easily.