Add visual in-context translation editing to PHP, Rails, Django, and other server-rendered applications
This repository demonstrates how to integrate Tolgee in-context translation editing into server-side rendered PHP applications. Enable translators and developers to edit translations directly in the browser - no code changes required.
Traditional PHP localization workflows require developers to:
- Find the translation key in code
- Look up the key in JSON/PO files
- Make changes
- Refresh to see results
With Tolgee's in-context editing, translators simply click on any text to edit it instantly.
| Feature | Description |
|---|---|
| βοΈ In-Context Editor | Click any text to translate - Alt+Click to edit |
| π Real-time Sync | Tolgee CLI watches and pulls translation changes automatically |
| π³ Docker Ready | One command setup with Docker Compose |
| π Multi-language | Built-in support for English, Czech, French, German |
This integration uses invisible Unicode markers to connect rendered text with translation keys:
Rendered HTML: "What To Pack" + invisible characters
β
Tolgee JS decodes: {"key": "app-title", "namespace": ""}
β
Result: Clickable, editable translation
- PHP renders translations with invisible key markers appended
- Tolgee Observer scans the DOM and detects marked strings
- User holds Alt/Option and clicks to open the translation editor
- Changes sync via Tolgee API to your translation management platform
- CLI pulls updates to your local JSON translation files
- Docker and Docker Compose
- Tolgee account (free tier available)
- Project API key from Tolgee
# Clone the PHP localization example
git clone https://github.com/tolgee/tolgee-php-demo.git
cd tolgee-php-demo
# Configure your Tolgee credentials
cat > .env << EOF
TOLGEE_API_URL=https://app.tolgee.io
TOLGEE_API_KEY=your-project-api-key
TOLGEE_DEV_MODE=true
EOF
# Start the application with translation sync
docker compose --profile dev up- Open http://localhost:8080
- Hold Alt (Windows/Linux) or Option (Mac)
- Hover over any translated text - it will highlight
- Click to open the translation editor
- Save changes - they sync automatically
tolgee-php-demo/
βββ docker-compose.yml # PHP 8.4 + Tolgee CLI services
βββ Dockerfile # Apache + PHP + SQLite
βββ .tolgeerc.json # Tolgee CLI configuration
β
βββ public/ # Web root
β βββ index.php # Main application with i18n
β βββ api.php # REST API endpoints
β βββ style.css # Application styles
β βββ i18n/ # Translation files
β βββ en.json # English translations
β βββ cs.json # Czech translations
β βββ fr.json # French translations
β βββ de.json # German translations
β
βββ src/ # PHP source
βββ Translator.php # Translation function with wrapping
βββ InvisibleWrapper.php # Invisible character encoding
βββ Database.php # SQLite database handler
The InvisibleWrapper class encodes translation keys using zero-width Unicode characters:
class InvisibleWrapper
{
// Zero-Width Non-Joiner (0) and Zero-Width Joiner (1)
const INVISIBLE_CHARACTERS = ["\u{200C}", "\u{200D}"];
public function wrap($key, $namespace, $translation)
{
// Encode {"k":"key","n":"namespace"} as invisible binary
$data = json_encode(['k' => $key, 'n' => $namespace ?: '']);
return $translation . $this->encodeToInvisible($data);
}
}These characters are:
- β Invisible to users
- β Safe in HTML
- β Preserved in copy/paste
- β Detected by Tolgee Observer
By default, you don't want to expose Tolgee dev tools or in-context editing capabilities to end users in production. The API key should never be bundled into client-side code, and the invisible character wrapping adds unnecessary overhead.
However, team members like translators or product managers often need to edit strings directly on production - seeing translations in their real context is invaluable for quality.
The Tolgee Browser Plugin solves this elegantly. It can inject the API key and dev tools directly into Tolgee JS on the client side, enabling in-context editing without any server-side credentials:
- Install the Tolgee Chrome Extension or Firefox Add-on
- Configure the plugin with your Tolgee API key
- Visit your app with
?tolgeeDevelopmentparameter to load Tolgee JS - The plugin injects credentials - in-context editing works without exposing keys server-side
This way, only team members with the plugin installed can edit translations, while regular users see a normal production site.
// Development mode is enabled when:
// - API key is provided (local development with .env)
// - ?tolgeeDevelopment query param is present (for browser plugin)
$isDevelopment = !empty($apiKey) || isset($_GET['tolgeeDevelopment']);Depending on your security requirements, consider these patterns:
// Enable via: /toggle-tolgee?enable=1
if (isset($_GET['enable'])) {
$_SESSION['tolgee_dev'] = $_GET['enable'] === '1';
}
$isDevelopment = $_SESSION['tolgee_dev'] ?? false;// Auto-enable on staging subdomains
$host = $_SERVER['HTTP_HOST'] ?? '';
$isDevelopment = str_contains($host, 'staging.')
|| str_contains($host, 'dev.')
|| $host === 'localhost';// Only enable for office/VPN IPs
$allowedIPs = ['192.168.1.0/24', '10.0.0.0/8'];
$isDevelopment = ipInRange($_SERVER['REMOTE_ADDR'], $allowedIPs);// Only for logged-in translators
$isDevelopment = isLoggedIn() && currentUser()->hasRole('translator');In production, development mode should be disabled to:
- Remove invisible character wrapping (cleaner HTML)
- Skip loading Tolgee JS (faster page loads)
Simply ensure no development triggers are active:
- No
TOLGEE_API_KEYin production env - Don't expose
?tolgeeDevelopmentparameter to end users
- π In-Context Editing for Backend Apps - Complete integration guide
- π Setup Guide - Step-by-step instructions
- π Tolgee CLI Documentation - CLI commands and configuration
- π Tolgee Platform Docs - Full documentation
- React + Tolgee - React SPA integration
- Next.js + Tolgee - Next.js with SSR
- Vue + Tolgee - Vue.js integration
- Svelte + Tolgee - Svelte integration
Contributions are welcome! Please read our Contributing Guide before submitting PRs.
Tolgee - Localization platform developers enjoy
Website β’
Documentation β’
Get Started Free