addweb/gtranslate-bundle
Symfony 7 bundle to integrate the GTranslate.io widget without copy‑pasting scripts. Configure via gtranslate.yaml, supports dropdown/float/dwf variants, provides a gtranslate_widget() Twig function with optional per-call overrides for script and settings.
Install the Bundle
composer require addweb/gtranslate-bundle:"*@dev"
Ensure AddWeb\GTranslateBundle\AddWebGTranslateBundle::class is registered in config/bundles.php.
Configure via config/packages/gtranslate.yaml
gtranslate:
script_src: 'https://cdn.gtranslate.net/widgets/latest/dwf.js' # Default: dark floating
auto_wrapper: true
settings:
default_language: 'en'
languages: ['en', 'fr', 'es']
First Use Case: Render the Widget in Twig
{{ gtranslate_widget() }}
Outputs the GTranslate widget with default settings.
Global Widget Integration
gtranslate_widget() in your base template (e.g., base.html.twig) to include the widget site-wide.auto_wrapper: true to auto-generate a container (e.g., <div class="gtranslate_wrapper">).Dynamic Overrides Override settings per template or partial:
{{ gtranslate_widget({
'script_src': 'https://cdn.gtranslate.net/widgets/latest/float.js',
'languages': ['en', 'de', 'ja'],
'wrapper_selector': '.custom-translator'
}) }}
Service-Based Usage
Inject the GTranslateService in controllers/services:
use AddWeb\GTranslateBundle\Service\GTranslateService;
public function __construct(private GTranslateService $gtranslate) {}
public function renderWidget(): string
{
return $this->gtranslate->render([
'script_src' => 'https://cdn.gtranslate.net/widgets/latest/dropdown.js',
]);
}
Environment-Specific Configs Use Symfony’s environment variables or parameter bags for multi-environment setups:
# config/packages/dev/gtranslate.yaml
gtranslate:
settings:
languages: ['en', 'fr'] # Dev-only languages
script_src CDN is whitelisted in your CSP headers if strict policies are enabled.{{ gtranslate_widget({'defer': true}) }}
Script Conflicts
script_src URL is correct and accessible. Test with browser dev tools (Network tab).https://cdn.gtranslate.net/...) instead of relative paths.Wrapper Selector Collisions
auto_wrapper: true and your template already has .gtranslate_wrapper, the widget may render incorrectly.wrapper_selector or disable auto_wrapper.Caching Issues
script_src: 'https://cdn.gtranslate.net/widgets/latest/dwf.js?v=2'
Twig Function Scope
gtranslate_widget() function is not available in PHP code directly. Use the service or pass data via Twig.404 errors on the script_src or missing gtranslate_widget Twig function.gtranslate bundle settings.Custom Styling
Override GTranslate’s CSS by targeting its classes (e.g., .gtranslate-switcher). Example:
.gtranslate-switcher {
right: 20px !important;
}
Exclude Pages Skip the widget on specific routes by conditionally rendering in Twig:
{% if app.request.get('_route') != 'admin.dashboard' %}
{{ gtranslate_widget() }}
{% endif %}
Extension Points Extend the Twig function or service:
// src/Twig/Extension/GTranslateExtension.php
class GTranslateExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('custom_gtranslate', [$this, 'renderCustomWidget']),
];
}
public function renderCustomWidget(array $options): string
{
// Custom logic (e.g., add analytics)
return (new GTranslateService())->render($options);
}
}
Testing Mock the service in PHPUnit:
$this->gtranslateService->expects($this->once())
->method('render')
->with(['script_src' => 'test.js']);
Fallback for No-JS Provide a static fallback for users with JavaScript disabled:
<noscript>
<div class="no-js-translator">
Translation unavailable. Enable JS or use {{ path('app.translate') }}.
</div>
</noscript>
{{ gtranslate_widget() }}
How can I help you explore Laravel packages today?