PHP Classes

PHP Google Translate Text Form: Show form to translate text with Google Translate

Recommend this page to a friend!
  Info   View files Documentation   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-27 (5 months ago) RSS 2.0 feedNot enough user ratingsTotal: 125 All time: 9,432 This week: 59Up
Version License PHP version Categories
php-google-translato 1.6GNU General Publi...5Localization, PHP 5, Web services
Description 

Author

This package can show a form to translate the text with Google Translate.

It can generate HTML and JavaScript that show a form that allows users to enter a text and choose languages and a button to let the users translate the text between two languages.

The class can also display flags for the countries of the available languages for the translations.

Innovation Award
PHP Programming Innovation award nominee
March 2022
Number 8
Google Translate is a popular service that allows anyone to translate text between two languages.

The original Google Translate service is made available on a Google site.

It is also possible to use the Google Translate service on another site by calling the Google Translate API.

This package provides an alternative to making Google Translate available on another site that uses JavaScript to call Google Translate service using Google JavaScript libraries to call the service.

Manuel Lemos
Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 25 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 11x

Documentation

php-google-translator

Php wrapper for google javascript translator website plugin. It will create a dropdown option for languages.

alt text alt text

Installation

Installation is super-easy via Composer:

composer require peterujah/php-google-translator

USAGES

Initalize with page languages

use Peterujah\NanoBlock\GTranslator;
$translate = new GTranslator("en", "/assets/flags/");

Or without icon path

use Peterujah\NanoBlock\GTranslator;
$translate = new GTranslator("en");

set selector design provider, you can choose between DEFAULT, SELECT or BOOTSTRAP. The DEFAULT is the default provider

$translate->setProvider(GTranslator::DEFAULT || GTranslator::SELECT || GTranslator::BOOTSTRAP);

Set languages icon path and icon type GTranslator::PNG || GTranslator::SVG. PNG or SVG to use icons download country language flag icon and set the Relative or Absolute path

$translate->setIconPath("https://foo.com/assets/flags/", GTranslator::PNG);

Or set individually by first setting path and then type to override the default type

$translate->setIconPath("/assets/flags/")->setIconType(GTranslator::PNG);

Adding additional language to translator

 $translate->addLanguage("en", "English")->addLanguage("ig", "Igbo");

Or load your languages to override the default

 $translate->setLanguages([
  "en" => "English",
  "ig" => "Igbo"
 ])

Display select option for languages, it accepts optional width.

 $translate->button(optional width = "50%");

When your provider is GTranslator::SELECT, button will return a html select option

 $translate->button();

To use image button, your provider must be GTranslator::DEFAULT

 $translate->imageButton();

Load supportes javascript plugin

 $translate->load();

Force translate page once pages is loaded, this must be called after $translate->load(); Method has deprecated and will be removed in future versions. Use preferredLanguage(string $key) instead.

$translate->forceLanguage("ms");

Set prefered language, this must be called after $translate->load();

$translate->preferredLanguage("ms");

Automatically detect browser language, this must be called after $translate->load();

$translate->autoTranslate();

Full usage on website to translate webpage

<?php 
 use Peterujah\NanoBlock\GTranslator;
 $translate = new GTranslator("en", "/assets/flags/");
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>PHP Google Translator</title>
  </head>
  <body>
      <div class="button">
        <?php $translate->button();?>
      </div>
      <div class="content">
        <h2>We have a long history of service in the Bay Area</h2>

        <p>
          We were one of the first credit unions that operate world wide, founded in 1932 as City &amp; County Employees' Credit Union. 
          Membership is now open to anyone who lives, works, or attends school in 
          Alameda, Contra Costa, San Joaquin, Solano, Stanislaus, or Kings counties in California. 
          We believe in banking locally and hope you will too. 
        </p>
      </div>
      <?php $translate->load();$translate->preferredLanguage("ms");?>
  </body>
</html>


Details

{ "name": "peterujah/php-google-translator", "description": "PHP Google Javascript Website Translator Plugin", "keywords": ["google", "translate", "translator", "php", "translating"], "license": "MIT", "type": "library", "homepage": "https://github.com/peterujah/php-google-translator", "authors": [ { "name": "Ujah Chigozie", "email": "ujahchigozie@gmail.com" } ], "require": { "php": "^7.0 || ^8" }, "autoload": { "psr-4": { "Peterujah\\NanoBlock\\": "src/" } }, "archive": { "exclude": ["assets"] }, "prefer-stable": true }

  Files folder image Files  
File Role Description
Files folder imageassets (2 files, 1 directory)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  assets  
File Role Description
Files folder imageflags (14 files)
  Accessible without login Image file cn.jpg Data Auxiliary data
  Accessible without login Image file en.jpg Data Auxiliary data

  Files folder image Files  /  assets  /  flags  
File Role Description
  Accessible without login Image file ar.png Icon Icon image
  Accessible without login Image file de.png Icon Icon image
  Accessible without login Image file en.png Icon Icon image
  Accessible without login Image file es.png Icon Icon image
  Accessible without login Image file fr.png Icon Icon image
  Accessible without login Image file id.png Icon Icon image
  Accessible without login Image file it.png Icon Icon image
  Accessible without login Image file ja.png Icon Icon image
  Accessible without login Image file ko.png Icon Icon image
  Accessible without login Image file ms.png Icon Icon image
  Accessible without login Image file nl.png Icon Icon image
  Accessible without login Image file pt.png Icon Icon image
  Accessible without login Image file ru.png Icon Icon image
  Accessible without login Image file zh-CN.png Icon Icon image

  Files folder image Files  /  src  
File Role Description
  Plain text file GTranslator.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:125
This week:0
All time:9,432
This week:59Up