All TalkersCode Topics

Follow TalkersCode On Social Media

Language Translator In PHP Source Code Demo

Last Updated : Jul 1, 2023

Language Translator In PHP Source Code Demo

In this article we will show you the solution of language translator in PHP source code demo, we created a language translator in php using google translate API. Now we used a PHP extension named cURL here. Let us understand the cURL first.

cURL: cURL stands for Client URL and is a library that lets you make HTTP requests.

cURL is a PHP extension allowing us to receive and send information via the URL syntax. By doing so cURL makes it easy to commination between different websites and domains.

Step By Step Guide On Language Translator In PHP Source Code Demo :-

$translation = $_POST['translation'];
$curl = curl_init();
curl_setopt_array($curl, [
 CURLOPT_POSTFIELDS => 'q='.$translation.'&target=es&source=en',
  "Accept-Encoding: application/gzip",
  "X-RapidAPI-Key: //API KEY HERE// ",
  "content-type: application/x-www-form-urlencoded"
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
 echo "cURL Error #:" . $err;
} else {
    $data = json_decode($response, true);
    $translated = $data['data']['translations'][0]['translatedText'];
    $translated = '';
<!DOCTYPE html>
<html lang = " en " >
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> language translator in php source code demo </title>
    <link rel = "stylesheet" href = "">
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> language translator in php source code demo </h2>
    <div class="card">
   <form action="language.php" method="post">
   <center><h3 style="color:green;">"<?php echo $translated ?>"</h3> </center>
    <input type="text" name="translation" id="text">
    <input type="submit" value="Translate">
  1. First using <?php tag to write php tag
  2. Using if statement with the isset function with the super global variable $_POST that is used to collect the form data.
  3. Again using $_POST method with the variable $translation.
  4. Using curl_init() to initialize a new session and return a cURL handle.
  5. Curl_setopt_array() sets multiple options for a cURL session. Set up the CURLOPT_URL to the google translate API.
  9. $response variable to curl_exec() function
  10. $err to curl_error() function.
  11. Using an if statement to display an error message. And an else statement with json_decode.
  12. Again use an else statement to show the translated text.
  13. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  14. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  15. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  16. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  17. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  18. <h1> tag used to add heading here and also adding the inline CSS here.
  19. Creating a <form> with the action with the file name.
  20. Now using a <center > and <h3> with a php code with an echo that will display the translated text
  21. <input> tag for the translation text
  22. Create an input of type Submit with the value translate
  23. Close </HTML> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know to language translator in php source.

I hope this article on language translator in PHP source code demo helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials