1

我正在将一个小型应用程序从 Symfony 迁移到一个单独的项目中,它所依赖的唯一 Symfony 组件是 I18N 字符串翻译。举例:

  • 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>
    

是否有类似的,最好兼容的系统,可作为 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>
    

    不幸的是,该<note>元素导致了错误消息,因此我将其删除。

  • 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>
    
4

1 回答 1

2

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

即使您没有在新项目中使用 Symfony,您也可以使用Symfony 组件翻译。Composer 和 autoload 将使它在您的常规 php 项目中可用。我推荐它。

于 2014-02-27T01:44:13.553 回答