A lightweight PHP wrapper for the Google Translate JavaScript plugin.
This library allows you to easily integrate a language selector into your website, supporting:
- Dropdown or image/button selectors for multiple languages.
- Automatic browser language detection and preferred language settings.
- Customizable UI with options for default, select, or Bootstrap-styled dropdowns.
- Flexible icon support with PNG or SVG flags from a local or remote path.
- Full control over CSS classes for containers and items.
Designed to simplify adding multilingual support to your site while keeping integration straightforward.
Installation is super-easy via Composer:
composer require peterujah/php-google-translatorWith a base path for flag icons:
use Peterujah\NanoBlock\GTranslator;
$translate = new GTranslator("en", "/assets/flags/");Without specifying an icon path:
$translate = new GTranslator("en");Choose between:
GTranslator::DEFAULT(default HTML dropdown or image button)GTranslator::SELECT(HTML select dropdown)GTranslator::BOOTSTRAP(Bootstrap dropdown)
$translate->setProvider(GTranslator::DEFAULT);
// or
$translate->setProvider(GTranslator::SELECT);
// or
$translate->setProvider(GTranslator::BOOTSTRAP);Set the relative or absolute path for your flag icons and choose between GTranslator::PNG or GTranslator::SVG:
$translate->setIconPath("/assets/flags/")->setIconType(GTranslator::PNG);Or using an external URL:
$translate->setIconPath("https://example.com/assets/flags/")->setIconType(GTranslator::PNG);Add additional languages individually:
$translate->addLanguage("en", "English")
->addLanguage("ig", "Igbo");Or override the default language map:
$translate->setLanguages([
"en" => "English",
"ig" => "Igbo",
]);Render the selector with an optional width (default "170px"):
$translate->button("50%");Behavior depends on the provider:
GTranslator::SELECT→ returns a<select>element.GTranslator::BOOTSTRAP→ returns a bootstrap dropdown<li>element.GTranslator::DEFAULT→ returns an image/button interface:
Image JS trigger button uses GTranslator::DEFAULT
$translate->setProvider(GTranslator::DEFAULT);
$translate->jsButton(true);Include the Google Translate JS and CSS:
$translate->load();Set the container class:
$translate->setContainerClass("my-translator");Set individual item classes:
$translate->setItemsClass("my-translator");Set a preferred language (must be after load()):
$translate->preferredLanguage("ms");Automatically detect the browser language (must be after load()):
$translate->autoTranslate();<?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>
