peterujah / php-google-translator
PHP Google Javascript Website Translator Plugin
Requires
- php: ^7.0 || ^8
README
Php wrapper for google javascript translator website plugin. It will create a dropdown option for languages.
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 translator javascript plugin
$translate->load();
Sets dropdown contemner class name.
$translate->setContainerClass("my-translator");
Sets dropdown items class name.
$translate->setItemsClass("my-translator");
Set preferred 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 & 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>