title: Twig: Working internationalization (i18n) example
body: <p>I am looking for a working i18n example for Twig (the template engine). </p>
  
  <p>Documentation is a bit sparse when it comes to language files. Where should they go, how should they look and what should they be named? I have been trying with .po/.mo files, no luck.</p>
  
  <p>If someone could point me in the right direction...</p>
  
  <p>See: <a href="http://www.twig-project.org/doc/extensions/i18n.html" rel="noreferrer">the i18n extension example</a> which does not really tell me much about the language files themselves.</p>
  
  <p>Note: I want to use Twig on its own, not as part of Symfony.</p>
  
  <p>Here is my php-file:</p>
  
  <pre><code> &lt;?php
  header('Content-Type: text/html; charset=utf-8');
  
  $vars = array();
  foreach($_POST as $key =&gt; $value) {
      $vars[$key] = json_decode(utf8_encode(urldecode($value)));
  }
  
  /* Prepare Twig template enginge */
  require_once './lib/Twig/Autoloader.php';
  Twig_Autoloader::register();
  
  $loader = new Twig_Loader_Filesystem('./templates');
  $twig = new Twig_Environment($loader, array(
      //'cache' =&gt; './cache',
      'cache' =&gt; false
  ));
  
  /* i18n */
  $twig-&gt;addExtension(new Twig_Extensions_Extension_I18n());
  
  $availableLanguages = array(
      'en' =&gt; 'en_EN',
      'de' =&gt; 'de_DE',
      'default' =&gt; 'de_DE'
  );
  
  // Set language
  $locale = array_key_exists($_GET['lang'], $availableLanguages) ? $availableLanguages[$_GET['lang']] : $availableLanguages['default'];
  putenv('LC_ALL='.$locale);
  setlocale(LC_ALL, $locale);
  
  // Specify the location of the translation tables
  bindtextdomain('kalkulator', 'includes/locale');
  bind_textdomain_codeset('kalkulator', 'UTF-8');
  
  // Choose domain
  textdomain('kalkulator');
  
  $template = $twig-&gt;loadTemplate('print.tpl');
  
  $html = $template-&gt;render($vars);
  
  switch($_GET['action']) {
      case 'mail':
  
      break;
      default:
          echo $html;
      break;
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>And inside includes/locale I have the following files:</p>
  
  <pre><code>-rw-r--r--  1 user group  670 Jul 28 10:17 kalkulator-de_DE.mo
  -rw-r--r--  1 user group  982 Jul 28 10:22 kalkulator-de_DE.po
  -rw-r--r--  1 user group  688 Jul 28 10:38 kalkulator-en_EN.mo
  -rw-r--r--  1 user group 1004 Jul 28 10:38 kalkulator-en_EN.po
  </code></pre>
  
  <p>And inside the print.tpl file I am using tags to specify which parts are to be translated:</p>
  
  <pre><code>{% trans %}
  Text to be translated
  {% endtrans %}
  </code></pre>