Php Symfony 1.4兼容的I18N翻译系统?
我正在将一个小应用程序从Symfony迁移到一个单独的项目中,并且 它所依赖的Symfony组件是I18N字符串转换。举例来说:Php Symfony 1.4兼容的I18N翻译系统?,php,internationalization,symfony1,symfony-1.4,xliff,Php,Internationalization,Symfony1,Symfony 1.4,Xliff,我正在将一个小应用程序从Symfony迁移到一个单独的项目中,并且 它所依赖的Symfony组件是I18N字符串转换。举例来说: action.class.php: $this->culture = 'pt_BR'; <h1><?php echo __('Destination') ?></h1> <?php require_once '../vendor/autoload.php'; use Symfony\Component\Tran
:action.class.php
$this->culture = 'pt_BR';
<h1><?php echo __('Destination') ?></h1>
<?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); }
<?php require_once('helper.inc.php') ?> <h1><?php echo __('Destination') ?></h1>
:模板/search_box.php
$this->culture = 'pt_BR';
<h1><?php echo __('Destination') ?></h1>
<?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); }
<?php require_once('helper.inc.php') ?> <h1><?php echo __('Destination') ?></h1>
xml
文件感兴趣,但我也可以
以不同的方式生活
更新
受@akky回答的启发,我现在得到了:
: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>
:public/header.php
$this->culture = 'pt_BR';
<h1><?php echo __('Destination') ?></h1>
<?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); }
<?php require_once('helper.inc.php') ?> <h1><?php echo __('Destination') ?></h1>
即使在新项目中未使用Symfony,也可以使用。Composer和autoload将使其在常规php项目中可用。我推荐它。我尝试了Symfony翻译组件,并跟进了,因为我无法让它工作。请参阅我的更新答案。使用Symfony 2的翻译组件有点奏效,但我不得不删除
标记。更新中的composer.json似乎有一些多余的行。如果您是软件包用户,通常只需要“需要”部分。当您制作并向其他人提供composer软件包时,可能会用到其他行。我在composer安装方面遇到了问题,但没有遵循依赖项。这就是为什么我添加了额外的字段,以使composer验证
满意。最后,我手动添加了symfony/config
,这是symfony/translation
所需要的。它不是作为依赖项自动安装的。