承接 matthiasnoback/microsoft-translator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

matthiasnoback/microsoft-translator

Composer 安装命令:

composer require matthiasnoback/microsoft-translator

包简介

Library for making calls to the Microsoft Translator V2 API

README 文档

README

By Matthias Noback (maintained by Alayn Gortazar)

Build Status

Installation

Using Composer, add to composer.json:

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

Then using the Composer binary:

php composer.phar install

Usage

This library uses the Buzz browser to make calls to the Microsoft Translator Text API 3.0.

You need to obtain a Microsoft Azure Cognitive Services subscription Key. This can be used to instantiate the AzureTokenProvider:

<?php

use MatthiasNoback\MicrosoftOAuth\AzureTokenProvider;
use MatthiasNoback\MicrosoftTranslator\MicrosoftTranslator;

use Buzz\Browser;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;


$client = new Curl(new Psr17Factory());
$browser = new Browser($client, new Psr17Factory());

$azureKey = '[YOUR-AZURE-SUBSCRIPTION-KEY]';

$accessTokenProvider = new AzureTokenProvider($browser, $azureKey);

$translator = new MicrosoftTranslator($browser, $accessTokenProvider);

Selecting azure token provider url

By default the acesstoken will be retrieved from https://api.cognitive.microsoft.com/sts/v1.0/issueToken , a third parameter can be passed to the AzureTokenProvider to choose another source.

<?php
// Some code

$accessTokenProvider = new AzureTokenProvider($browser, $azureKey, 'https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken');

Optional: enable the access token cache

Each call to the translator service is preceded by a call to Microsoft's OAuth server. Each access token however, may be cached for 10 minutes, so you should also use the built-in AccessTokenCache:

<?php

use MatthiasNoback\MicrosoftOAuth\AccessTokenCache;
use Cache\Adapter\PHPArray\ArrayCachePool;

$cache = new ArrayCachePool();
$accessTokenCache = new AccessTokenCache($cache);
$accessTokenProvider->setCache($accessTokenCache);

The actual cache provider can be anything, as long as it implements the CachePoolInterface interface from FIG-PSR6

Making calls

Translate a string

<?php
// Some code

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

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

Translate a string and get multiple translations

<?php
// Some code

$matches = $translator->getTranslations('This is a test', 'nl', 'en');

foreach ($matches as $match) {
    // $match is an instance of MatthiasNoback\MicrosoftTranslator\ApiCall\TranslationMatch
    $degree = $match->getDegree();
    $translatedText = $match->getTranslatedText();
}

Detect the language of a string

<?php
// Some code

$text = 'This is a test';

$detectedLanguage = $translator->detect($text);

// $detectedLanguage will be 'en'

Tests

Take a look at the tests to find out what else you can do with the API.

To fully enable the test suite, you need to copy phpunit.xml.dist to phpunit.xml and replace the placeholder values with their real values (i.e. client id, client secret and a location for storing spoken text files).

Build Status

TODO

  • There are some more calls to be implemented, and also some more tests to be added.

统计信息

  • 总下载量: 234.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 4
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固