Php Symfony 1.4兼容的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

我正在将一个小应用程序从Symfony迁移到一个单独的项目中,并且 它所依赖的Symfony组件是I18N字符串转换。举例来说:

  • 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>
    
是否有类似的、最好是兼容的系统作为PHP包提供

我对保存带有翻译的
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>
    
    
    

资源文件格式的名称为XLIFF。您可能会发现XLIFF处理php包,以及xliff2(您最喜欢的i18n格式)转换器


即使在新项目中未使用Symfony,也可以使用。Composer和autoload将使其在常规php项目中可用。我推荐它。

我尝试了Symfony翻译组件,并跟进了,因为我无法让它工作。请参阅我的更新答案。使用Symfony 2的翻译组件有点奏效,但我不得不删除
标记。更新中的composer.json似乎有一些多余的行。如果您是软件包用户,通常只需要“需要”部分。当您制作并向其他人提供composer软件包时,可能会用到其他行。我在
composer安装方面遇到了问题,但没有遵循依赖项。这就是为什么我添加了额外的字段,以使
composer验证
满意。最后,我手动添加了
symfony/config
,这是
symfony/translation
所需要的。它不是作为依赖项自动安装的。