Skip to content
A Twig Translation Extension
Branch: master
Clone or download
Latest commit d5d3ec6 Apr 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Twig Changed Twig class names and namespaces Apr 12, 2019
tests Changed Twig class names and namespaces Apr 12, 2019
.cs.php Changed Twig class names and namespaces Apr 12, 2019
.editorconfig Init Nov 26, 2017
.gitattributes Init Nov 26, 2017
.gitignore Init Nov 26, 2017
.scrutinizer.yml Changed Twig class names and namespaces Apr 12, 2019
.styleci.yml Init Nov 26, 2017
.travis.yml Changed Twig class names and namespaces Apr 12, 2019
LICENSE Init Nov 26, 2017
README.md Updated Apr 12, 2019
build.xml Fix build Apr 12, 2019
composer.json Fix build Apr 12, 2019
phpstan.neon Init Nov 26, 2017
phpunit.xml Fixed some phpstan issues Feb 11, 2018

README.md

Twig Translation Extension

A Twig Translation Extension.

Latest Version on Packagist Software License Build Status Code Coverage Scrutinizer Code Quality Total Downloads

Installation

composer require odan/twig-translation

Integration

Register the Twig Extension

$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, array(
    'cache' => '/path/to/twig-cache',
));

$twig->addExtension(new \Odan\Twig\TwigTranslationExtension());

Register a callback function

Create a global callback function with the name __.

This example uses the symfony/translation component:


use Symfony\Component\Translation\Translator;

/**
 * Text translation (I18n)
 *
 * @param string|Translator $message
 * @return string
 *
 * <code>
 * echo __('Hello');
 * echo __('There are %s users logged in.', 7);
 * </code>
 */
function __($message)
{
    /** @var Translator $translator */
    static $translator = null;
    
    if ($message instanceof Translator) {
        $translator = $message;
        return '';
    }

    $translated = $translator->trans($message);
    $context = array_slice(func_get_args(), 1);
    
    if (!empty($context)) {
        $translated = vsprintf($translated, $context);
    }
    
    return $translated;
}

Usage

Translate a text:

{{ __('Yes') }}

Translate a text with a placeholder:

{{ __('Hello: %s', username) }}

Output (depends on the language):

Hello master

Translate a text with multiple placeholders:

{{ __('First name: %s, Last name: %s', firstName, lastName) }}

Output (depends on the language):

First name: John, Last name: Doe

Create a plural translation:

Example 1:

{% if count > 1 %}
    {{ count }} {{ __('Users') }}
{% else %}
    {{ count }} {{ __('User') }}
{% endif %}

Example 2:

{% if users|length > 1 %}
    {{ users|length }} {{ __('Users') }}
{% else %}
    {{ users|length }} {{ __('User') }}
{% endif %}

Create a complex plural translation:

{% if not count %}
    {{ __('No users') }}
{% elseif count = 1 %}
    {{ count }} {{ __('User') }}
{% else %}
    {{ count }} {{ __('Users') }}
{% endif %}

Parsing with Poedit

The workflow

  1. Parse all twig files (php bin/parse-twig.php)
  2. Start Poedit and open the .po file
  3. Click the Update button to parse all PHP and Twig cache files
  4. 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)
  • Click the OK button and Update 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.

File: bin/parse-twig.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

// Specify our Twig templates location
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/../templates');

 // Instantiate Twig
$twig = new \Twig\Environment($loader);

// Compile all Twig templates into the cache directory
$cachePath = __DIR__ . '/../tmp/twig-cache';
$compiler = new \Odan\Twig\TwigCompiler($twig, $cachePath);
$compiler->compile();

echo "Done\n";

To run this script just enter: php bin/parse-twig.php

You can’t perform that action at this time.