Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Janma Kundali
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Artificial Intelligence (1)
  • Automobiles (12)
    • Cars (7)
  • Blog (107)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (396)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Janma Kundali
  • Gadgets
    • Phones
    • Android Phones
Close

Search

Free Google language translation API using PHP
PHP

Free Google language translation API using PHP

By Admin
June 6, 2023 2 Min Read
Comments Off on Free Google language translation API using PHP

Language translation refers to the process of converting written or spoken text from one language into another language while preserving its meaning. It plays a crucial role in bridging the communication gap between individuals who speak different languages and enables the exchange of information and ideas across cultural and linguistic boundaries.

Language translation using programming involves leveraging software and algorithms to automate the translation process. Here are a few common approaches:

  • API-Based Translation Services
  • Machine Translation Libraries
  • Natural Language Processing (NLP) Libraries
  • Rule-Based Translation
  • Neural Machine Translation (NMT) Models

The following PHP script provides one of the easiest way to achieve language translation using Google API.

Follow this video for complete guidance :

PHP Function to call Google API for Translation

function translate($text,$source_lang='en',$target_lang='es'){
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => 'sl='.$source_lang.'&tl='.$target_lang.'&q='.urlencode($text),
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/x-www-form-urlencoded'
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);

  $sentencesArray = json_decode($response, true);
  $sentences = "";
  foreach ($sentencesArray[0] as $s) {
        $sentences .= isset($s[0]) ? $s[0] : '';
    }
  
  return $sentences;
}

Complete source code

<?php

function translate($text,$source_lang='en',$target_lang='es'){
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => 'sl='.$source_lang.'&tl='.$target_lang.'&q='.urlencode($text),
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/x-www-form-urlencoded'
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);

  $sentencesArray = json_decode($response, true);
  $sentences = "";
  foreach ($sentencesArray[0] as $s) {
        $sentences .= isset($s[0]) ? $s[0] : '';
    }
  
  return $sentences;
}

if(isset($_POST['text'])){
  echo translate($_POST['text']);
  die;
}else{
  $output = translate('hello');
}

?>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>

<div class="container mt-5">
  <div class="row">
    <div class="col-6">
      <p class="mb-3" style="font-weight: bold;">English</p>
      <textarea class="form-control input" style="resize:none;" rows="20">Hello</textarea>
    </div>
    <div class="col-6">
      <p class="mb-3" style="font-weight: bold;">English</p>
      <textarea class="form-control output" style="resize:none;" rows="20" readonly><?php echo $output;?></textarea>
    </div>
    <div class="col-12">
      <span class="btn-translate btn btn-primary d-block btn-block mt-3">Convert</span>
    </div>
  </div>
</div>


<script type="text/javascript">
  $(".btn-translate").on('click',function(){
    inputtext = $(".input").val();
    postData = {
      'text':inputtext
    };
    $.ajax({
      url:'index.php',
      type:'post',
      data:postData,
      success:function(res){
        $(".output").html(res);
      }
    });
  });
</script>

 

Author

Admin

Follow Me
Other Articles
Remove HTML extension for clean URLs using htaccess
Previous

Remove HTML extension for clean URLs using htaccess

Threads vs Twitter : 10 Points that separates the two
Next

Threads vs Twitter : 10 Points that separates the two

Recent Posts

  • What REALLY Happens When You Type a URL and Press Enter?
  • Balen Shah UN Speech 2026: Full Text & Audio UN General Assembly
  • inDrive Provides Safe Driving Training and Certificates to 1,500+ Driver Partners in Nepal
  • NEPSE’s Data Center Faces Technical Issue: Today’s Share Trading Suspended
  • KIST Teaching Hospital Ranks First in Public Health Office’s Evaluation

Tags

adsense ai animation animation using HTML and CSS API blog calculator chatgpt clock Cryptocurrency CSS css animation custom file manager Cyber Security design Email Facebook featured filemanager free template google htaccess HTML image Instagram interview javascript JQuery NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App QR random react reactjs Rotate seo travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • What REALLY Happens When You Type a URL and Press Enter? Admin
  • Balen Shah UN Speech 2026: Full Text & Audio UN General Assembly Admin
  • inDrive Provides Safe Driving Training and Certificates to 1,500+ Driver Partners in Nepal Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme