antfroger/dont-translate-bundle
composer require antfroger/dont-translate-bundle
AppKernel.php:
$bundles[] = new Af\Bundle\DontTranslateBundle\AfDontTranslateBundle();
config/packages/af_dont_translate.yaml:
af_dont_translate:
mode: "get" # or "cookie"
param_name: "untrans"
roles: ["ROLE_ADMIN", "ROLE_TRANSLATOR"]
?untrans to any URL to see raw translation keys instead of translated strings. Example:
http://your-app.dev/admin/dashboard?untrans
This is invaluable for:
Development Workflow:
mode: "get" for quick debugging during development.?untrans=1).Production Workflow:
mode: "cookie" for production-like environments.roles (e.g., only admins/translators).// Set via JavaScript or browser dev tools
document.cookie = "untrans=true; path=/";
Integration with Twig:
{{ 'welcome.message'|trans }} will show welcome.message when untrans is active.API/CLI Usage:
Translator service directly:
$translator = $this->get('translator');
$translator->trans('key'); // Returns raw key if untrans is active
<button onclick="document.cookie='untrans={{ isUntransActive ? 'false' : 'true' }}; path=/'; location.reload();">
{{ isUntransActive ? 'Disable' : 'Enable' }} Raw Keys
</button>
config/packages/dev/af_dont_translate.yaml):
af_dont_translate:
mode: "get" # Only GET mode in dev
debug_keys) to avoid conflicts:
af_dont_translate:
param_name: "debug_keys"
Role-Based Access:
roles are configured, unauthenticated users cannot trigger the feature, even with the GET parameter or cookie.ROLE_USER or no roles if you want public access:
af_dont_translate:
roles: [] # No roles = public access
Cookie Scope:
param_name is unique to avoid unintended side effects (e.g., conflicting with other bundles).Caching:
cache:pool:clear), ensure translations are re-warmed after toggling untrans.Symfony 4+ Compatibility:
TranslatorInterface event listeners.Nested Translations:
['key' => 'value']). If your translations use complex structures, test thoroughly.Check Active State:
untrans is active:
{{ dump(app.request.query.get('untrans') || app.request.cookies.get('untrans')) }}
$this->get('af_dont_translate.listener')->isActive();
Override Translator Globally:
# config/services.yaml
services:
App\Service\CustomTranslator:
decorates: 'translator'
arguments: ['@App\Service\CustomTranslator.inner']
Then implement logic to check untrans in translate() method.Clear Cookies:
curl -X POST --cookie "untrans=" http://your-app.dev
Or use browser dev tools to delete the cookie manually.Custom Logic:
AfDontTranslateBundle\EventListener\TranslatorListener to add custom conditions:
// src/EventListener/CustomTranslatorListener.php
use Af\Bundle\DontTranslateBundle\EventListener\TranslatorListener;
class CustomTranslatorListener extends TranslatorListener {
public function isActive() {
if (parent::isActive()) {
return true;
}
// Add custom logic (e.g., IP whitelist)
return $this->request->getClientIp() === '127.0.0.1';
}
}
config/services.yaml:
services:
Af\Bundle\DontTranslateBundle\EventListener\TranslatorListener:
class: App\EventListener\CustomTranslatorListener
UI Indicators:
untrans is active:
{% if app.request.query.get('untrans') or app.request.cookies.get('untrans') %}
<div class="untrans-badge">🔍 RAW KEYS MODE</div>
{% endif %}
Environment Variables:
// config/packages/af_dont_translate.yaml
af_dont_translate:
mode: "%env(UNTRANS_MODE)%" # Defaults to "get" if not set
How can I help you explore Laravel packages today?