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.

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/addweb/gtranslate-bundle

0.1 2025-10-14 07:44 UTC

This package is auto-updated.

Last update: 2025-10-14 07:58:37 UTC


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.