A simple PHP project that demonstrates integration with the Tolgee translation API and invisible text encoding, running in Docker.
index.php
: A PHP file that displays "Hello, World!", translation examples, and invisible encoding examplesTranslator.php
: A class that handles translation using the Tolgee APIInvisibleWrapper.php
: A class that wraps text with invisible Unicode charactersDockerfile
: Configuration for building a Docker image with PHP and Apachedocker-compose.yml
: Configuration for running the Docker container
- Docker
- Docker Compose
- Clone this repository
- Navigate to the project directory
- Set up your Tolgee API key using one of the methods described in the Tolgee API Configuration section
- For quick setup, copy the
.env.example
file to.env
and add your API key
- For quick setup, copy the
- Run the following command:
docker-compose up -d
- Open your browser and go to http://localhost:8999
- You should see the translation examples displayed in your browser
To stop the Docker container, run:
docker-compose down
The project is set up with a volume mount, so any changes you make to the PHP files will be immediately reflected when you refresh the browser.
Tolgee is configured to run in development mode, which enables features like in-context translation editing and key highlighting. Development mode is enabled by:
- Using the development version of the Tolgee JavaScript SDK:
<script src="https://cdn.jsdelivr.net/npm/@tolgee/web/dist/tolgee-web.development.umd.js"></script>
- Setting the
development
parameter totrue
in the JavaScript configuration:
const tolgee = Tolgee()
.use(DevTools())
.use(ObserverPlugin())
.use(DevBackend())
.init({
// other configuration...
development: true
})
- Setting the
mode
parameter to'development'
in the PHP configuration:
$config = [
// other configuration...
'mode' => 'development'
];
When development mode is enabled, the Translator class will wrap translations with invisible encoded key data, which allows the Tolgee JavaScript SDK to identify and highlight translation keys in the browser.
This project uses the Tolgee translation management platform. To configure the Tolgee API integration:
- Sign up for a Tolgee account at https://app.tolgee.io/sign_up
- Create a new project
- (optional) Add some keys for testing
- Generate an API key with read permissions for your project
- Set your API key as an environment variable:
Uncomment and update the environment variable in docker-compose.yml
:
environment:
TOLGEE_API_KEY: your_api_key_here
You can also set the environment variable before running the application:
# Linux/macOS
export TOLGEE_API_KEY=your_api_key_here
docker-compose up -d
# Windows (Command Prompt)
set TOLGEE_API_KEY=your_api_key_here
docker-compose up -d
# Windows (PowerShell)
$env:TOLGEE_API_KEY="your_api_key_here"
docker-compose up -d
Copy the provided .env.example
file to create your own .env
file:
cp .env.example .env
Then edit the .env
file to add your actual API key:
TOLGEE_API_KEY=your_api_key_here
The docker-compose.yml file is already configured to use the .env file:
environment:
TOLGEE_API_KEY: ${TOLGEE_API_KEY}
Note: The .env
file is included in .gitignore
to prevent accidentally committing your API key to the repository.
The Translator class will automatically fetch translations from the Tolgee API when needed.
The Tolgee API is expected to return translations in the following format:
{
"en": {
"what a key": "Translated value",
"another key": "Another key translated"
},
"cs": {
"what a key": "Překlad",
"another key": "Další překlad"
}
}
Where:
- The top-level keys are language codes
- Each language contains key-value pairs of translation keys and their translated values
- The Translator class will use the translations for the language specified in the configuration
The project includes an InvisibleWrapper
class that can wrap text with invisible Unicode characters. This can be useful for:
- Embedding metadata in HTML without affecting the visual appearance
- Creating watermarks in text content
- Implementing steganography techniques in web pages
- Tracking the origin of copied content
The wrapper uses two special Unicode characters (Zero-Width Non-Joiner and Zero-Width Joiner) that are invisible when rendered in browsers. The wrapping process:
- Converts the input text to bytes
- Converts each byte to an 8-bit binary representation
- Adds a separator bit after each byte
- Maps each bit to one of the invisible characters
- Appends the invisible characters to the original text
// Include the class
require_once 'InvisibleWrapper.php';
// Create an instance
$encoder = new InvisibleWrapper();
// Encode a message
$message = "Hello, World!";
$encodedMessage = $encoder->encodeMessage($message);
// The encoded message can be added to any HTML element
echo "<p>This text contains invisible data" . $encodedMessage . "</p>";
The encoded message is completely invisible when rendered in a browser but can be detected and decoded by JavaScript or other tools.