Skip to content

Commit db37d34

Browse files
committed
init
0 parents  commit db37d34

File tree

15 files changed

+680
-0
lines changed

15 files changed

+680
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_size = 4
8+
indent_style = space

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/composer.lock
3+
/vendor

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Mario
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Twig Trans
2+
3+
[![Latest Version](https://img.shields.io/github/release/JBlond/twig-trans.svg?style=flat-square&label=Release)](https://github.com/JBlond/twig-trans/releases)
4+
5+
## Introduction
6+
7+
This is the replacement for the old **Twig** Extensions **I18n** / **Intl** for the translation with po / mo
8+
**gettext** files.
9+
10+
I didn't wanted to Symfony, but Twig only. Symfony seemed to be too much overhead.
11+
12+
This extension enable Twig templates to use `|trans` and `{% trans %}` + `{% endtrans %}` again
13+
14+
## Install
15+
16+
```shell
17+
composer require jblond/twig-trans
18+
```
19+
20+
## Example Use
21+
22+
```PHP
23+
<?php
24+
25+
use jblond\TwigTrans\Translation;
26+
use Twig\Environment;
27+
use Twig\Loader\FilesystemLoader;
28+
use Twig\TwigFilter;
29+
30+
require '../vendor/autoload.php';
31+
32+
$langCode = 'de_DE';
33+
putenv("LC_ALL=$langCode.UTF-8");
34+
if (setlocale(LC_ALL, "$langCode.UTF-8") === false) {
35+
sprintf('Language Code %s not found', $langCode);
36+
}
37+
38+
// set the path to the translation
39+
bindtextdomain("Web_Content", "./locale");
40+
41+
// choose Domain
42+
textdomain("Web_Content");
43+
44+
$twigConfig = array(
45+
'cache' => false,
46+
'debug' => true,
47+
'auto_reload' => true
48+
);
49+
50+
$twigLoader = new FilesystemLoader('./tpl/');
51+
$twig = new Environment($twigLoader, $twigConfig);
52+
53+
// this is for the filter |trans
54+
$filter = new TwigFilter('trans', function (Environment $env, $context, $string) {
55+
return Translation::TransGetText($string, $context);
56+
}, ['needs_context' => true, 'needs_environment' => true]);
57+
58+
// load the i18n extension for using the translation tag for twig
59+
// {% trans %}my string{% endtrans %}
60+
$twig->addFilter($filter);
61+
$twig->addExtension(new Translation());
62+
63+
try {
64+
$tpl = $twig->load('default.twig');
65+
} catch (Exception $exception) {
66+
echo $exception->getMessage();
67+
return;
68+
}
69+
70+
$tpl->render();
71+
```
72+
73+
74+
## Requirements
75+
76+
* PHP 7.2 or greater
77+
* PHP Multibyte String
78+
' gettext'
79+
80+
81+
### License (MIT License)
82+
83+
see [License](LICENSE)
84+
85+
## Tests
86+
87+
```bash
88+
composer run-script php_src
89+
```

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name" : "jblond/twig-trans",
3+
"type" : "library",
4+
"description" : "",
5+
"license" : "MIT",
6+
"keywords" : [
7+
"php",
8+
"twig",
9+
"trans",
10+
"translation",
11+
"endtrans",
12+
"po file"
13+
],
14+
"authors" : [{
15+
"name" : "Mario",
16+
"email" : "[email protected]"
17+
}
18+
],
19+
"require" : {
20+
"php" : ">=7.2",
21+
"ext-gettext": "*",
22+
"ext-mbstring": "*",
23+
"twig/twig": ">=3.0.0"
24+
},
25+
"require-dev" : {
26+
"squizlabs/php_codesniffer" : "*"
27+
},
28+
"autoload" : {
29+
"psr-4" : {
30+
"jblond\\" : "src/jblond"
31+
}
32+
},
33+
"config" : {
34+
"classmap-authoritative" : true
35+
},
36+
"scripts" : {
37+
"php_src" : "phpcs --standard=phpcs.xml -s -p --colors ./src/"
38+
}
39+
}

example/example.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use jblond\TwigTrans\Translation;
4+
use Twig\Environment;
5+
use Twig\Loader\FilesystemLoader;
6+
use Twig\TwigFilter;
7+
8+
require '../vendor/autoload.php';
9+
10+
error_reporting(E_ALL);
11+
ini_set('display_errors', 'On');
12+
13+
$langCode = 'de_DE';
14+
putenv("LC_ALL=$langCode.UTF-8");
15+
if (setlocale(LC_ALL, "$langCode.UTF-8") === false) {
16+
sprintf('Language Code %s not found', $langCode);
17+
}
18+
19+
// set the path to the translation
20+
bindtextdomain("Web_Content", "./locale");
21+
22+
// choose Domain
23+
textdomain("Web_Content");
24+
25+
$twigConfig = array(
26+
'cache' => false,
27+
'debug' => true,
28+
'auto_reload' => true
29+
);
30+
31+
$twigLoader = new FilesystemLoader('./tpl/');
32+
$twig = new Environment($twigLoader, $twigConfig);
33+
34+
// this is for the filter |trans
35+
$filter = new TwigFilter('trans', function (Environment $env, $context, $string) {
36+
return Translation::transGetText($string, $context);
37+
}, ['needs_context' => true, 'needs_environment' => true]);
38+
39+
// load the i18n extension for using the translation tag for twig
40+
// {% trans %}my string{% endtrans %}
41+
$twig->addFilter($filter);
42+
$twig->addExtension(new Translation());
43+
44+
try {
45+
$tpl = $twig->load('default.twig');
46+
} catch (Exception $exception) {
47+
echo $exception->getMessage();
48+
die();
49+
}
50+
51+
echo $tpl->render();
332 Bytes
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: product-information-rekovelle\n"
4+
"Language-Team: German\n"
5+
"MIME-Version: 1.0\n"
6+
"Content-Type: text/plain; charset=UTF-8\n"
7+
"Content-Transfer-Encoding: 8bit\n"
8+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
9+
"Language: de_DE\n"
10+
11+
#. Test
12+
#: test id
13+
msgid "my string"
14+
msgstr "Meine Zeichenkette"
323 Bytes
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: product-information-rekovelle\n"
4+
"Language-Team: German\n"
5+
"MIME-Version: 1.0\n"
6+
"Content-Type: text/plain; charset=UTF-8\n"
7+
"Content-Transfer-Encoding: 8bit\n"
8+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
9+
"Language: en_EN\n"
10+
11+
#. Test
12+
#: test id
13+
msgid "my string"
14+
msgstr "my string"

0 commit comments

Comments
 (0)