Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlond committed Jan 16, 2020
0 parents commit db37d34
Show file tree
Hide file tree
Showing 15 changed files with 680 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/composer.lock
/vendor
21 changes: 21 additions & 0 deletions LICENSE
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.
89 changes: 89 additions & 0 deletions README.md
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
```
39 changes: 39 additions & 0 deletions composer.json
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/"
}
}
51 changes: 51 additions & 0 deletions example/example.php
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 added example/locale/de_DE/Web_Content.mo
Binary file not shown.
14 changes: 14 additions & 0 deletions example/locale/de_DE/Web_Content.po
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 added example/locale/en_EN/Web_Content.mo
Binary file not shown.
14 changes: 14 additions & 0 deletions example/locale/en_EN/Web_Content.po
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"
20 changes: 20 additions & 0 deletions example/tpl/default.twig
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>
6 changes: 6 additions & 0 deletions phpcs.xml
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>
Loading

0 comments on commit db37d34

Please sign in to comment.