Download the PHP package phpmyadmin/twig-i18n-extension without Composer

On this page you can find all versions of the php package phpmyadmin/twig-i18n-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package twig-i18n-extension

Twig i18n Extension

The i18n extension adds gettext support to Twig. It defines one tag, trans.

Code status

Tests Code coverage

Installation

This library can be installed via Composer running the following from the command line:

composer require phpmyadmin/twig-i18n-extension

Configuration

You need to register this extension before using the trans block

use PhpMyAdmin\Twig\Extensions\I18nExtension;

$twig->addExtension(new I18nExtension());

Note that you must configure the gettext extension before rendering any internationalized template. Here is a simple configuration example from the PHP documentation

// Set language to French
putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');

// Specify the location of the translation tables
bindtextdomain('myAppPhp', 'includes/locale');
bind_textdomain_codeset('myAppPhp', 'UTF-8');

// Choose domain
textdomain('myAppPhp');

Usage

Use the trans block to mark parts in the template as translatable:

{% trans "Hello World!" %}

{% trans string_var %}

{% trans %}
    Hello World!
{% endtrans %}

In a translatable string, you can embed variables:

{% trans %}
    Hello {{ name }}!
{% endtrans %}

During the gettext lookup these placeholders are converted. {{ name }} becomes %name% so the gettext msgid for this string would be Hello %name%!.

{% trans "Hello {{ name }}!" %} is not a valid statement.

If you need to apply filters to the variables, you first need to assign the result to a variable:

{% set name = name|capitalize %}

{% trans %}
    Hello {{ name }}!
{% endtrans %}

To pluralize a translatable string, use the plural block:

{% trans %}
    Hey {{ name }}, I have one apple.
{% plural apple_count %}
    Hey {{ name }}, I have {{ count }} apples.
{% endtrans %}

The plural tag should provide the count used to select the right string. Within the translatable string, the special count variable always contain the count value (here the value of apple_count).

To add notes for translators, use the notes block:

{% trans %}
    Hey {{ name }}, I have one apple.
{% plural apple_count %}
    Hey {{ name }}, I have {{ count }} apples.
{% notes %}
    This is shown in the user menu. This string should be shorter than 30 chars
{% endtrans %}

You can use notes with or without plural. Once you get your templates compiled you should configure the gettext parser to get something like this: xgettext --add-comments=notes

Within an expression or in a tag, you can use the trans filter to translate simple strings or variables:

{{ var|default(default_value|trans) }}

Complex Translations within an Expression or Tag

Translations can be done with both the trans tag and the trans filter. The filter is less powerful as it only works for simple variables or strings. For more complex scenario, like pluralization, you can use a two-step strategy:

{# assign the translation to a temporary variable #}
{% set default_value %}
    {% trans %}
      Hey {{ name }}, I have one apple.
    {% plural apple_count %}
      Hey {{ name }}, I have {{ count }} apples.
    {% endtrans %}
{% endset %}

{# use the temporary variable within an expression #}
{{ var|default(default_value|trans) }}

Extracting Template Strings

If you use the Twig I18n extension, you will probably need to extract the template strings at some point.

Using Poedit 2

Poedit 2 has native support for extracting from Twig files and no extra setup is necessary (Pro version).

Using xgettext or Poedit 1

Unfortunately, the xgettext utility does not understand Twig templates natively and neither do tools based on it such as free versions of Poedit. But there is a simple workaround: as Twig converts templates to PHP files, you can use xgettext on the template cache instead.

Create a script that forces the generation of the cache for all your templates. Here is a simple example to get you started

use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use PhpMyAdmin\Twig\Extensions\I18nExtension;

$tplDir = __DIR__ . '/templates';
$tmpDir = '/tmp/cache/';
$loader = new FilesystemLoader($tplDir);

// force auto-reload to always have the latest version of the template
$twig = new Environment($loader, [
    'auto_reload' => true,
    'cache' => $tmpDir,
]);
$twig->addExtension(new I18nExtension());
// configure Twig the way you want

// iterate over all your templates
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
{
    // force compilation
    if ($file->isFile()) {
        $twig->loadTemplate(str_replace($tplDir . '/', '', $file));
    }
}

Use the standard xgettext utility as you would have done with plain PHP code:

xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP /tmp/cache/*.php

Another workaround is to use Twig Gettext Extractor and extract the template strings right from Poedit.

History

This project was forked in 2019 by the phpMyAdmin team, since it was abandoned by the Twig project but was still in use for phpMyAdmin.

If you find this work useful, or have a pull request to contribute, please find us on Github.


All versions of twig-i18n-extension with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2 || ^8.0
twig/twig Version ^1.42.3|^2.0|^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package phpmyadmin/twig-i18n-extension contains the following files

Loading the files please wait ....