문제

나는 작은 앱을 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>
    
도움이 되었습니까?

해결책

리소스 파일 형식의 이름은 XLIFF입니다.XLIFF 처리 PHP 패키지와 xliff2(가장 선호하는 i18n 형식) 변환기를 찾을 수 있습니다.

당신은 사용할 수 있습니다 Symfony 구성 요소 번역 새 프로젝트에서 Symfony를 사용하지 않더라도 마찬가지입니다.Composer와 자동 로드를 통해 일반 PHP 프로젝트에서 사용할 수 있습니다.난 그것을 추천 해.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top