1

I am migrating a small app from Symfony into a separate project, and the only Symfony component that it depends on is I18N string translation. By example:

  • action.class.php:

    $this->culture = 'pt_BR';
    
  • templates/search_box.php:

    <h1><?php echo __('Destination') ?></h1>
    
  • i18n/pt_BR/messages.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <xliff version="1.0">
      <file datatype="plaintext" source-language="en"
            target-language="pt_BR" original="messages">
        <body>
          <note>
            SEARCH BOX
          </note>
          <trans-unit id="0">
            <source>Destination</source>
            <target>Destino</target>
          </trans-unit>
        </body>
      </file>
    </xliff>
    

Is there a similar, preferably compatible system, available as a PHP package?

I am interested in keeping the xml file with translations, but I could also live with a different format.

Update

What I have now, inspired by @akky's answer:

  • composer.json:

    {
        "name": "example/layout",
        "description":
        "Provides common layout components such as header / footer.",
        "license": "proprietary",
        "require": {
            "symfony/translation": "2.4.*",
            "symfony/config": "~2.0"
        }
    }
    
  • i18n/messages.pt_BR.xliff:

    <?xml version="1.0" encoding="utf-8"?>
    <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
      <file source-language="en" datatype="plaintext" original="messages">
        <body>
          <trans-unit id="0">
            <source>Destination</source>
            <target>Destino</target>
          </trans-unit>
        </body>
      </file>
    </xliff>
    

    Unfortunately, the <note> element caused an error messages, and so I removed it.

  • public/helper.inc.php:

    <?php
    
    require_once '../vendor/autoload.php';
    
    use Symfony\Component\Translation\Translator;
    use Symfony\Component\Translation\MessageSelector;
    use Symfony\Component\Translation\Loader\ArrayLoader;
    use Symfony\Component\Translation\Loader\XliffFileLoader;
    use Symfony\Component\Config\Resource\FileResource;
    
    $loader = new XliffFileLoader();
    $catalogue = $loader->load('../i18n/messages.pt_BR.xliff', 'pt_BR');
    
    function __($text) {
      global $catalogue;
      return $catalogue->get($text);
    }
    
  • public/header.php:

    <?php require_once('helper.inc.php') ?>
    
    <h1><?php echo __('Destination') ?></h1>
    
feklee
  • 7,555
  • 9
  • 54
  • 72

1 Answers1

2

The name of the resource file format is XLIFF. You may find XLIFF handling php packages, as well as xliff2(your favorite i18n format) converters.

You may use Symfony Components Translation even if you are not using Symfony in the new project. Composer and autoload will make it available on your regular php project. I recommend it.

akky
  • 2,818
  • 21
  • 31
  • I tried the Symfony Translation component, and followed up with [another question](http://stackoverflow.com/questions/22063982), because I couldn't get it to work. – feklee Feb 27 '14 at 09:19
  • 1
    See my updated answer. Using Symfony 2's translation component sort of worked, but I had to remove the `` tag. – feklee Feb 27 '14 at 17:01
  • That composer.json in your update seems to have some redundant lines. If you are a package user, you usually need only "require" parts. Other lines may be for when you make and offer a composer package to others. – akky Feb 28 '14 at 00:42
  • I had issues with `composer install` not following dependencies. That's why I added the extra fields, to make `composer validate` happy. Eventually, I manually added `symfony/config`, which is needed by `symfony/translation`. It was not automatically installed as dependency. – feklee Feb 28 '14 at 09:39