matthiasnoback/microsoft-translator-bundle

Microsoft Translator V2 API 的打包工具

v0.2.2 2015-11-19 21:41 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:19 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"

进行调用

翻译字符串

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