Language Translator In PHP Source Code Demo
Last Updated : Mar 11, 2024
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>
- First using <?php tag to write php tag
- Using if statement with the isset function with the super global variable $_POST that is used to collect the form data.
- Again using $_POST method with the variable $translation.
- Using curl_init() to initialize a new session and return a cURL handle.
- Curl_setopt_array() sets multiple options for a cURL session. Set up the CURLOPT_URL to the google translate API.
- Set the CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION to true
- Set some CURLOPT_HTTPHEADER
- Set CURLOPT_CUSTOMREQUEST to POST
- $response variable to curl_exec() function
- $err to curl_error() function.
- Using an if statement to display an error message. And an else statement with json_decode.
- Again use an else statement to show the translated text.
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Creating a <form> with the action with the file name.
- Now using a <center > and <h3> with a php code with an echo that will display the translated text
- <input> tag for the translation text
- Create an input of type Submit with the value translate
- 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.