peterujah/php-google-translator
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.
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 & 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>
peterujah/php-google-translator 适用场景与选型建议
peterujah/php-google-translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 465 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「google」 「translator」 「translate」 「translating」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 peterujah/php-google-translator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 peterujah/php-google-translator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 peterujah/php-google-translator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Olympus Hermes is a bundle used to translate all messages from your WordPress packages and Olympus bundles.
Alfabank REST API integration
This is a simple light weight language parser to help with language key translation built for speed using in-memory caching optimization technique.
Simple and fast translator and tools to internationalize your PHP application.
统计信息
- 总下载量: 465
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-24

