Download the PHP package odan/twig-translation without Composer
On this page you can find all versions of the php package odan/twig-translation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download odan/twig-translation
More information about odan/twig-translation
Files in odan/twig-translation
Package twig-translation
Short Description A Twig Translation Extension
License MIT
Homepage https://github.com/odan/twig-translation
Informations about the package twig-translation
Twig Translation Extension
A Twig Translation Extension.
Please read this first!
The symfony/twig-bridge also provides a
Twig 3 TranslationExtension
to translate messages with the trans filter.
For this reason the odan/twig-translation
component is just redundant and will be deprecated in the near future.
I strongly recommend you to use the symfony/twig-bridge
TranslationExtension instead.
Here you can find an installation guide:
Requirements
- PHP 7.3+ or 8.0+
Installation
Registering the extension
This example uses the symfony/translation component:
Register the Twig Extension:
Slim 4 integration
To install the symfony/translation component, run:
Add settings:
Add a new container definition:
Create a global translation function
This step is optional, but recommend if you want to translate messages directly in PHP.
Create the file src/Utility/translate.php
and copy / paste this content:
Register the composer autoloader in composer.json:
Run: composer update
Usage
Translate a text:
Translate a text with a placeholder:
Output (depends on the language):
Translate a text with multiple placeholders:
Output (depends on the language):
Create a plural translation:
Example 1:
Example 2:
Create a complex plural translation:
Parsing with Poedit
The workflow
- Parse all twig files (
php bin/parse-twig.php
) - Start Poedit and open the .po file
- Click the
Update
button to parse all PHP and Twig cache files - Translate the text and save the file.
Poedit Setup
- Start Poedit and open the .po file
- Open the menu:
Catalogue
>Properties...
- Open the tab:
Source paths
- Add a new path and point it to the twig cache
- The path must be relative to the base path e.g.
..\temp\twig-cache
- Open the tab:
Source keyword
- Add a new keyword with the name
__
(2 underscores)
- Add a new keyword with the name
- Click the
OK
button andUpdate
the calalogue.
Parsing Twig files
You need to iterate and compile all your Twig templates. The compilation step generates the PHP cache files that can be parsed from Poedit. This script is only an example and must be adapted to your individual environment.
Twig settings:
File: bin/parse-twig.php
To run this script just enter: php bin/parse-twig.php
Similar libraries
The symfony/twig-bridge provides TranslationExtension
for Twig 3+.
Read more
License
- MIT