Use a Symfony 2 component in a project other than Symfony?

To get support for XLIFF / 2 in PHP, in another answer , it was suggested to use the Symfony 2 translation component.

So I downloaded it from Github to a directory ../vendor/and naively tried using it:

<?php

    require_once '../vendor/Symfony/Component/Translation/Translator.php';
    require_once '../vendor/Symfony/Component/Translation/MessageSelector.php';
    require_once '../vendor/Symfony/Component/Translation/Loader/ArrayLoader.php';

    use Symfony\Component\Translation\Translator;
    use Symfony\Component\Translation\MessageSelector;
    use Symfony\Component\Translation\Loader\ArrayLoader;

    $translator = new Translator('fr_FR', new MessageSelector());

This does not work as other components need to be loaded:

PHP Fatal error:  Interface 'Symfony\\Component\\Translation\\TranslatorInterface' not found in /home/ec2-user/layout/vendor/Symfony/Component/Translation/Translator.php on line 25

Now I can manually add require_oncefor each file until all the dependencies, but I'm not sure if this is the right approach.

How to use one Symfony 2 component in a project other than Symfony? It is a bad idea?

+4
source share
2 answers

.

composer.json :

{
    "require": {
        "symfony/translation": "2.4.*"
    }
}

:

wget http://getcomposer.org/composer.phar
php composer.phar install

, :

<?php

    require_once('vendor/autoload.php');

    use Symfony\Component\Translation\Translator;
    use Symfony\Component\Translation\MessageSelector;
    use Symfony\Component\Translation\Loader\ArrayLoader;

    $translator = new Translator('fr_FR', new MessageSelector());
    $translator->setFallbackLocales(array('fr'));
    $translator->addLoader('array', new ArrayLoader());
    $translator->addResource('array', array(
        'Hello World!' => 'Bonjour',
    ), 'fr');

    echo $translator->trans('Hello World!')."\n";
+7

Composer .

, ,

,

#

, Composer , , . , :

require 'vendor/autoload.php';
+3

Source: https://habr.com/ru/post/1529170/


All Articles