All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Language Translator In PHP Source Code Demo

Last Updated : Mar 11, 2024

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 :-

<?php
if(isset($_POST['submit'])){
$translation = $_POST['translation'];
$curl = curl_init();
curl_setopt_array($curl, [
 CURLOPT_URL => "https://google-translate1.p.rapidapi.com/language/translate/v2",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => 'q='.$translation.'&target=es&source=en',
 CURLOPT_HTTPHEADER => [
  "Accept-Encoding: application/gzip",
  "X-RapidAPI-Host: google-translate1.p.rapidapi.com",
  "X-RapidAPI-Key: //API KEY HERE// ",
  "content-type: application/x-www-form-urlencoded"
 ],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
} else {
    $data = json_decode($response, true);
    $translated = $data['data']['translations'][0]['translatedText'];
}
}else{
    $translated = '';
}
?>
<!DOCTYPE html>
<html lang = " en " >
<head>
    <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 = "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <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">
    </div>
   </form>
</body>
</html>
  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.
  6. Set the CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION to true
  7. Set some CURLOPT_HTTPHEADER
  8. Set CURLOPT_CUSTOMREQUEST to POST
  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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪