问题描述
要在PHP中获得XLIFF/2支持,请在另一个答案中,建议执行以下操作:使用Symfony 2 Translation组件.
For getting XLIFF/2 support in PHP, in another answer, it was suggested touse the Symfony 2 Translation component.
所以我从Github 将它下载到目录../vendor/
中,并且天真地尝试使用它:
So I downloaded it from Github into a directory ../vendor/
and naivelytried to use 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 doesn’t work as other components would 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
现在,我可以为每个文件手动添加一个require_once
,直到所有依赖关系得到满足,但是我不确定这是否是正确的方法.
如何在非Symfony项目中使用单个Symfony 2组件?那是一个好主意吗?
How do I use a single Symfony 2 component in a non-Symfony project? Is that abad idea?
推荐答案
首先在您的项目文件夹中创建一个composer.json
文件:
First create a composer.json
file in your project folder :
{
"require": {
"symfony/translation": "2.4.*"
}
}
Then download composer and run it :
wget http://getcomposer.org/composer.phar
php composer.phar install
You can now use your component by importing the composer autoloader :
<?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";
这篇关于在非Symfony项目中使用Symfony 2组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!