-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit db37d34
Showing
15 changed files
with
680 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_size = 4 | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
/composer.lock | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Mario | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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) | ||
|
||
## Introduction | ||
|
||
This is the replacement for the old **Twig** Extensions **I18n** / **Intl** for the translation with po / mo | ||
**gettext** files. | ||
|
||
I didn't wanted to Symfony, but Twig only. Symfony seemed to be too much overhead. | ||
|
||
This extension enable Twig templates to use `|trans` and `{% trans %}` + `{% endtrans %}` again | ||
|
||
## Install | ||
|
||
```shell | ||
composer require jblond/twig-trans | ||
``` | ||
|
||
## Example Use | ||
|
||
```PHP | ||
<?php | ||
|
||
use jblond\TwigTrans\Translation; | ||
use Twig\Environment; | ||
use Twig\Loader\FilesystemLoader; | ||
use Twig\TwigFilter; | ||
|
||
require '../vendor/autoload.php'; | ||
|
||
$langCode = 'de_DE'; | ||
putenv("LC_ALL=$langCode.UTF-8"); | ||
if (setlocale(LC_ALL, "$langCode.UTF-8") === false) { | ||
sprintf('Language Code %s not found', $langCode); | ||
} | ||
|
||
// set the path to the translation | ||
bindtextdomain("Web_Content", "./locale"); | ||
|
||
// choose Domain | ||
textdomain("Web_Content"); | ||
|
||
$twigConfig = array( | ||
'cache' => false, | ||
'debug' => true, | ||
'auto_reload' => true | ||
); | ||
|
||
$twigLoader = new FilesystemLoader('./tpl/'); | ||
$twig = new Environment($twigLoader, $twigConfig); | ||
|
||
// this is for the filter |trans | ||
$filter = new TwigFilter('trans', function (Environment $env, $context, $string) { | ||
return Translation::TransGetText($string, $context); | ||
}, ['needs_context' => true, 'needs_environment' => true]); | ||
|
||
// load the i18n extension for using the translation tag for twig | ||
// {% trans %}my string{% endtrans %} | ||
$twig->addFilter($filter); | ||
$twig->addExtension(new Translation()); | ||
|
||
try { | ||
$tpl = $twig->load('default.twig'); | ||
} catch (Exception $exception) { | ||
echo $exception->getMessage(); | ||
return; | ||
} | ||
|
||
$tpl->render(); | ||
``` | ||
|
||
|
||
## Requirements | ||
|
||
* PHP 7.2 or greater | ||
* PHP Multibyte String | ||
' gettext' | ||
|
||
|
||
### License (MIT License) | ||
|
||
see [License](LICENSE) | ||
|
||
## Tests | ||
|
||
```bash | ||
composer run-script php_src | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name" : "jblond/twig-trans", | ||
"type" : "library", | ||
"description" : "", | ||
"license" : "MIT", | ||
"keywords" : [ | ||
"php", | ||
"twig", | ||
"trans", | ||
"translation", | ||
"endtrans", | ||
"po file" | ||
], | ||
"authors" : [{ | ||
"name" : "Mario", | ||
"email" : "[email protected]" | ||
} | ||
], | ||
"require" : { | ||
"php" : ">=7.2", | ||
"ext-gettext": "*", | ||
"ext-mbstring": "*", | ||
"twig/twig": ">=3.0.0" | ||
}, | ||
"require-dev" : { | ||
"squizlabs/php_codesniffer" : "*" | ||
}, | ||
"autoload" : { | ||
"psr-4" : { | ||
"jblond\\" : "src/jblond" | ||
} | ||
}, | ||
"config" : { | ||
"classmap-authoritative" : true | ||
}, | ||
"scripts" : { | ||
"php_src" : "phpcs --standard=phpcs.xml -s -p --colors ./src/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
use jblond\TwigTrans\Translation; | ||
use Twig\Environment; | ||
use Twig\Loader\FilesystemLoader; | ||
use Twig\TwigFilter; | ||
|
||
require '../vendor/autoload.php'; | ||
|
||
error_reporting(E_ALL); | ||
ini_set('display_errors', 'On'); | ||
|
||
$langCode = 'de_DE'; | ||
putenv("LC_ALL=$langCode.UTF-8"); | ||
if (setlocale(LC_ALL, "$langCode.UTF-8") === false) { | ||
sprintf('Language Code %s not found', $langCode); | ||
} | ||
|
||
// set the path to the translation | ||
bindtextdomain("Web_Content", "./locale"); | ||
|
||
// choose Domain | ||
textdomain("Web_Content"); | ||
|
||
$twigConfig = array( | ||
'cache' => false, | ||
'debug' => true, | ||
'auto_reload' => true | ||
); | ||
|
||
$twigLoader = new FilesystemLoader('./tpl/'); | ||
$twig = new Environment($twigLoader, $twigConfig); | ||
|
||
// this is for the filter |trans | ||
$filter = new TwigFilter('trans', function (Environment $env, $context, $string) { | ||
return Translation::transGetText($string, $context); | ||
}, ['needs_context' => true, 'needs_environment' => true]); | ||
|
||
// load the i18n extension for using the translation tag for twig | ||
// {% trans %}my string{% endtrans %} | ||
$twig->addFilter($filter); | ||
$twig->addExtension(new Translation()); | ||
|
||
try { | ||
$tpl = $twig->load('default.twig'); | ||
} catch (Exception $exception) { | ||
echo $exception->getMessage(); | ||
die(); | ||
} | ||
|
||
echo $tpl->render(); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: product-information-rekovelle\n" | ||
"Language-Team: German\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: nplurals=2; plural=(n != 1);\n" | ||
"Language: de_DE\n" | ||
|
||
#. Test | ||
#: test id | ||
msgid "my string" | ||
msgstr "Meine Zeichenkette" |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: product-information-rekovelle\n" | ||
"Language-Team: German\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: nplurals=2; plural=(n != 1);\n" | ||
"Language: en_EN\n" | ||
|
||
#. Test | ||
#: test id | ||
msgid "my string" | ||
msgstr "my string" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta name="robots" content="noindex"> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>PHP JBlond Twig Trans</title> | ||
</head> | ||
<body> | ||
|
||
|
||
{% trans %}my string{% endtrans %}<br><br> | ||
|
||
{{ 'my string'|trans }}<br><br> | ||
THE END | ||
|
||
<div style="clear:both;"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ruleset name="twig-trans"> | ||
<description>twig-trans</description> | ||
<rule ref="PSR12" /> | ||
<file>src</file> | ||
</ruleset> |
Oops, something went wrong.