maxvanceffer/microsoft-translator-bundle

用于使用Microsoft Translator V2 API的组件包(分支以便使用symfony 2.4进行检出)

v0.2.1 2013-07-16 07:22 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:10:45 UTC


README

作者:Matthias Noback

安装

使用Composer,在composer.json中添加

{
    "require": {
        "matthiasnoback/microsoft-translator-bundle": "dev-master"
    }
}

然后使用Composer二进制文件

php composer.phar install

/app/AppKernel.php中注册组件包

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new MatthiasNoback\MicrosoftTranslatorBundle\MatthiasNobackMicrosoftTranslatorBundle(),
        );
    }
}

使用方法

此组件包封装了对应的Microsoft Translator V2 API PHP库,并将翻译器作为服务microsoft_translator添加到您的服务容器中。

您需要在Azure DataMarket注册您的应用程序,并因此获取“客户端ID”和“客户端密钥”。将这些值复制到config.yml中的正确密钥

matthias_noback_microsoft_translator:
    oauth:
        client_id: "YOUR-CLIENT-ID"
        client_secret: "YOUR-CLIENT-SECRET"

调用API

翻译字符串

// in your controller

$translatedString = $this->get('microsoft_translator')->translate('This is a test', 'nl', 'en');

// $translatedString will be 'Dit is een test', which is Dutch for...

检测字符串的语言

$text = 'This is a test';

$detectedLanguage = $this->get('microsoft_translator')->detect($text);

// $detectedLanguage will be 'en'

获取字符串的语音版本

$text = 'My name is Matthias';

$spoken = $this->get('microsoft_translator')->speak($text, 'en', 'audio/mp3', 'MaxQuality');

// $spoken will be the raw MP3 data, which you can save for instance as a file

更多示例,请参阅PHP库的README