peterujah/php-google-translator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

peterujah/php-google-translator

最新稳定版本:3.1

Composer 安装命令:

composer require peterujah/php-google-translator

包简介

PHP Google Javascript Website Translator Plugin

README 文档

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>

统计信息

  • 总下载量: 368
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固