addweb/gtranslate-bundle
最新稳定版本:0.1
Composer 安装命令:
composer require addweb/gtranslate-bundle
包简介
A Symfony bundle that integrates the GTranslate.io widget, providing a Twig function and configurable service to easily add automatic website translation.
README 文档
README
A Symfony 7.x bundle that integrates the GTranslate.io widget.
It provides a Twig function and service to render the GTranslate widget with fully configurable settings — so you don’t need to copy-paste raw <script> snippets.
🚀 Features
- Easy integration of the GTranslate.io widget.
- Configurable via Symfony config (
config/packages/gtranslate.yaml). - Supports all widget variants: dropdown, floating, dark floating (
dwf.js). - Twig function
gtranslate_widget()to drop the widget anywhere in your templates. - Optional overrides per call — change languages, script, or wrapper at runtime.
- Unit & integration tests included.
📦 Installation
Require the bundle in your Symfony app:
composer require addweb/gtranslate-bundle:"*@dev"
⚙️ Configuration
Create config/packages/gtranslate.yaml in your Symfony app:
gtranslate: script_src: 'https://cdn.gtranslate.net/widgets/latest/dwf.js' auto_wrapper: true settings: default_language: 'en' languages: ['en','fr','it','es'] wrapper_selector: '.gtranslate_wrapper' switcher_horizontal_position: 'right' switcher_vertical_position: 'top' switcher_text_color: '#f7f7f7' switcher_arrow_color: '#f2f2f2' switcher_border_color: '#161616' switcher_background_color: '#303030' switcher_background_shadow_color: '#474747' switcher_background_hover_color: '#3a3a3a' dropdown_text_color: '#eaeaea' dropdown_hover_color: '#748393' dropdown_background_color: '#474747'
-
script_src → Which GTranslate script to load (dropdown.js, float.js, dwf.js).
-
settings → Directly mirrors window.gtranslateSettings.
-
auto_wrapper → If true, a
wrapper is auto-generated based on wrapper_selector.
🖥️ Usage
Twig
Add the widget in any template:
{# Default (uses bundle config) #}
{{ gtranslate_widget() }}
{# Override script + settings at runtime #}
{{ gtranslate_widget({
script_src: 'https://cdn.gtranslate.net/widgets/latest/float.js',
settings: {
default_language: 'en',
languages: ['en','hi'],
switcher_horizontal_position: 'left',
switcher_vertical_position: 'bottom'
},
auto_wrapper: false
}) }}
Controller
You can render it from a controller as well:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use AddWeb\GtranslateBundle\Service\GtranslateWidgetRenderer;
final class GtranslateDemoController extends AbstractController
{
#[Route('/gt-demo', name: 'gt_demo')]
public function demo(GtranslateWidgetRenderer $renderer): Response
{
return new Response('<html><body>'.$renderer->render().'</body></html>');
}
}
✅ Verify
Check the Twig function is registered:
bin/console debug:twig | grep gtranslate_widget
Load your app in a browser and you should see the widget, and text should translate when you pick a language.
🧪 Tests
Run the test suite in the bundle directory:
vendor/bin/phpunit
Tests cover:
- The renderer’s HTML output.
- Per-call overrides.
- DI wiring with a test kernel.
📜 License
This bundle is under the MIT license. For the whole copyright, see the LICENSE file distributed with this source code.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-13