Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jblond/twig-trans
Twig 3 translation extension
https://github.com/jblond/twig-trans
gettext i18n i18nextension intl multitype translation twig twig-extension twig-templates twig-trans
Last synced: 15 days ago
JSON representation
Twig 3 translation extension
- Host: GitHub
- URL: https://github.com/jblond/twig-trans
- Owner: JBlond
- License: mit
- Created: 2020-01-16T08:25:50.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-29T10:06:12.000Z (6 months ago)
- Last Synced: 2024-10-17T04:23:55.236Z (29 days ago)
- Topics: gettext, i18n, i18nextension, intl, multitype, translation, twig, twig-extension, twig-templates, twig-trans
- Language: PHP
- Homepage: https://github.com/JBlond/twig-trans
- Size: 37.1 KB
- Stars: 12
- Watchers: 6
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Twig Trans
[![Latest Version](https://img.shields.io/github/release/JBlond/twig-trans.svg?style=flat-square&label=Release)](https://github.com/JBlond/twig-trans/releases)
[![Packagist Installs](https://badgen.net/packagist/dt/jblond/twig-trans)](https://packagist.org/packages/jblond/twig-trans)## Introduction
This is the replacement for the old **Twig** Extensions **I18n** / **Intl** / **I18nExtension** for the translation with po / mo
**gettext** files.I didn't want to install Symfony, but Twig only. Symfony seemed to be too much overhead.
This extension enables Twig templates to use `|trans` and `{% trans %}` + `{% endtrans %}` again
## Install
```shell
composer require jblond/twig-trans
```## Example Use
```PHP
false,
'debug' => true,
'auto_reload' => true
];$twigLoader = new FilesystemLoader('./tpl/');
$twig = new Environment($twigLoader, $twigConfig);
// if you need {{ dump() }} in the twig templates add
//$twig->addExtension(new DebugExtension());
// and use Twig\Extension\DebugExtension; // at the top of the file// this is for the filter |trans
$filter = new TwigFilter(
'trans',
function ($context, $string) {
return Translation::transGetText($string, $context);
},
['needs_context' => true]
);
$twig->addFilter($filter);// load the i18n extension for using the translation tag for twig
// {% trans %}my string{% endtrans %}
$twig->addExtension(new Translation());try {
$tpl = $twig->load('default.twig');
} catch (Exception $exception) {
echo $exception->getMessage();
die();
}// the array parameter (context) is optional for the render function
echo $tpl->render(['key1' => 'value1', 'key2' => 'value2']);
```## Requirements
* PHP 7.2 or greater
* PHP Multibyte String ' gettext'
* Twig >= 3.0### Optional Requirements
* xgettext for Extract / generating po files.
### License (MIT License)
see [License](LICENSE)
## Tests
```bash
composer run-script php_src
composer run-script php_test
composer run-script phpunit
```## Contribution, wishes and bug
Raise an [issue](https://github.com/JBlond/twig-trans/issues)