peterujah/php-google-translator

PHP Google Javascript Website Translator Plugin

Installs: 354

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 2

Open Issues: 0

pkg:composer/peterujah/php-google-translator

3.1 2026-02-23 13:33 UTC

This package is auto-updated.

Last update: 2026-02-23 14:07:51 UTC


README

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.

alt text alt text

Installation

Installation is super-easy via Composer:

composer require peterujah/php-google-translator

Usage

Initialize the translator

With 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");

Set the selector provider

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);

Configure icon path and type

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);

Manage languages

Add additional languages individually:

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

Or override the default language map:

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

Display the language selector

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);

Load the translator engine

Include the Google Translate JS and CSS:

$translate->load();

Customize classes

Set the container class:

$translate->setContainerClass("my-translator");

Set individual item classes:

$translate->setItemsClass("my-translator");

Control language behavior

Set a preferred language (must be after load()):

$translate->preferredLanguage("ms");

Automatically detect the browser language (must be after load()):

$translate->autoTranslate();

Full example

<?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>