2

I am trying to use teh Twig i18n Extension.

As far as I can tell the file I need is here:

https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/I18n.php

Now I am not quite sure where to save this file

I have Twig in a folder called includes/lib (/includes/lib/Twig). I see a folder Extension under Twig. Do I save it here?

After I save it, do I need to do a "require_once" to the file or will Twig_Autoloader do the job for me?

I am not using Symfony2

Thanks Craig

4

3 回答 3

4

Here is the complete answer that worked for me:

  1. Copy the file in Twig-Verzeichnis (extract i18n.zip in Twig). For the I18n extension it would be Twig/Extensions/Extension/I18n.php
  2. Eventually add other files requred by I18n. You will see what these are by the error messages that come. I had to add "Twig/Extensions/Node/Trans.php" and "Twig/Extensions/TokenParser/Trans.php".
  3. In your config file add the following:

    // Set language to German
    putenv('LC_ALL=de_DE'); 
    setlocale(LC_ALL, 'de_DE'); 
    // Specify location of translation tables
    bindtextdomain("project_de_DE", "./locale"); 
    // Choose domain 
    textdomain("projectl_de_DE");
    
  4. Register the Twig Extension

    $twig->addExtension(new Twig_Extensions_Extension_I18n());
    
  5. Create the directory locale/de_DE/LC_MESSAGES

  6. Create the PO file (the easisest is to have a sample file to start from)
  7. Open the file in a normal text editor (be sure to use utf-8 encoding) and start translating
  8. Open the PO-Datei with PoEdit (www.poedit.net/)
  9. Save to locale/de_DE/LC_MESSAGES (a MO-Datei will be created).
  10. Add the translation to the appropriate places in the Twig-Template with

    {% trans 'Text in the original language' %}`
    
于 2012-10-18T09:08:18.137 回答
0

You need to register this extension with Twig:

$twig->addExtension(new Twig_Extensions_Extension_I18n());

If your installation is configured correctly, the autoloader should do the job of including the file. If not, you could include the file manually or make the installation with composer.

于 2012-10-03T08:03:50.717 回答
0

It seems the "proper" way to install these extensions without Composer is as follows:

  1. Download a release from https://github.com/fabpot/Twig-extensions/releases
  2. Copy the contents of the lib/ directory somewhere to your project
  3. include the file .../Twig/Extensions/Autoloader.php
  4. Register autoloader: Twig_Extensions_Autoloader::register();
  5. Continue as explained in the doc: http://twig.sensiolabs.org/doc/extensions/i18n.html
于 2014-03-22T18:23:07.507 回答