knone/translate-bundle

翻译包

安装次数: 24

依赖关系: 0

建议者: 0

安全性: 0

星标: 4

关注者: 2

分支: 0

类型:symfony-bundle

v1.0.0 2014-06-29 16:38 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:29:39 UTC


README

此包适用于 Symfony2 应用程序,使用不同的翻译服务翻译应用程序中的某些文本

Latest Stable Version Total Downloads License

Build Status Scrutinizer Code Quality

安装

使用 composer 下载

php composer.phar require knone/translate-bundle '1.0.*'

在内核中启用此包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new KNone\TranslateBundle\KNoneTranslateBundle(),
    );
}

配置 KNoneTranslateBundle

KNoneTranslateBundle 有两个翻译提供者

  1. google_web (使用 http://translate.google.com)
  2. yandex_api (http://api.yandex.com/translate/)

您需要在 config.yml 中设置默认提供者

//app/config/config.yml
k_none_translate:
    default_provider: google_web

如果您想使用 Yandex Api,您需要设置 API 密钥 (获取 API 密钥)

//app/config/config.yml
k_none_translate:
    default_provider: yandex_api
    providers:
        yandex_api:
            key: <api_key>

使用 KNoneTranslateBundle

您只需使用服务 k_none_translate.translator。示例

<?php
...
// Some action in controller
public function someAction()
{
    /** @var KNone\TranslateBundle\Provider\ProviderInterface $translator */
    $translator = $this->get('k_none_translate.translator');
    $translation = $translator->translate('hello world', 'en', 'fr');
    // you can set 'auto' as source language and translator will detect it
    //$translation = $translator->translate('hello world', 'auto', 'fr');

    $result = (string)$translation; // $result contains 'bonjour tout le monde'
    $result = $translation->getResult(); // $result contains 'bonjour tout le monde'
    $source = $translation->getSource(); // $source contains 'hello world'
    $sourceLanguage = $translation->getSourceLanguage() // $sourceLanguage contains 'en'
    $resultLanguage = $translation->getResultLanguage() // $resultLanguage contains 'fr'
}