# DeepLy
[![Build Status](https://img.shields.io/travis/chriskonnertz/DeepLy.svg)](https://travis-ci.org/chriskonnertz/DeepLy)
[![Version](https://img.shields.io/packagist/v/chriskonnertz/DeepLy.svg)](https://packagist.org/packages/chriskonnertz/deeply)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/chriskonnertz/deeply/master/LICENSE)
[DeepL.com](https://www.deepl.com/) is a next-generation translation service.
It provides better translations compared to other popular translation engines.
DeepLy is a PHP package that implements a client to interact with DeepL via their API _without_ an API key.
~Please switch to DeepLy 2 if you have an API key.~
> **IMPORTANT statement regarding DeepL Pro**: DeepLy 2 is under development but it is unkown when it will be released. It will support DeepL Pro.
## Installation
Through [Composer](https://getcomposer.org/):
```
composer require chriskonnertz/deeply
```
From then on you may run `composer update` to get the latest version of this library.
It is possible to use this library without using Composer but then it is necessary to register an
[autoloader function](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md#example-implementation).
> This library requires PHP 5.6 or higher and the cURL extension.
## Usage Example
```php
$deepLy = new ChrisKonnertz\DeepLy\DeepLy();
$translatedText = $deepLy->translate('Hello world!', 'DE', 'EN');
echo $translatedText; // Prints "Hallo Welt!"
```
> An interactive PHP demo script is included. It is located at `demos/demo_translate.php`.
### Sophisticated Example
```php
use ChrisKonnertz\DeepLy\DeepLy;
$deepLy = new DeepLy();
try {
$translatedText = $deepLy->translate('Hello world!', DeepLy::LANG_EN, DeepLy::LANG_AUTO);
echo $translatedText; // Prints "Hallo Welt!"
} catch (\Exception $exception) {
echo $exception->getMessage();
}
```
Always wrap calls of the `translate` method in a try-catch-block, because they might throw an exception if the
arguments are invalid or the API call fails. Instead of using hardcoded strings as language arguments
better use the language code constants of the `DeepLy` class. The class also offers methods such as
`getLangCodes($withAuto = true)` and `supportsLangCode($langCode)`.
You may use the `proposeTranslations` method if you want to get alternative translations for a text.
This method cannot operate on more than one sentence at once.
## Auto-Detect Language
DeepLy has a method that uses the DeepL API to detect the language of a text:
```php
$languageCode = $deepLy->detectLanguage('Hello world!');
```
This will return 'EN'. The language of the text has to be one of the supported languages or the result will be incorrect.
If you do not need the code of the language but its name, you may call the `$deepLy->getLangName($langCode)` method.
The API, in general, can handle and completely translate texts that contain parts with different languages,
if the language switch is not within a sentence. The `detectLanguage()` method will however
only return the code of _one_ language. It will throw an exception if it is unable to auto-detect the language.
This will rarely happen, it is more likely that the API will return a "false positive": It will rather detect the wrong
language than no language at all.
> An interactive PHP demo script is included. It is located at `demos/demo_detect.php`.
## Supported Languages
DeepL(y) supports these languages:
| Code | Language |
|------|---------------|
| auto | _Auto detect_ |
| DE | German |
| EN | English |
| FR | French |
| ES | Spanish |
| IT | Italian |
| NL | Dutch |
| PL | Polish |
| PT | Portuguese |
| RU | Russian |
> Note that auto-detection only is possible for the source language.
DeepL says they will [add more languages](https://www.heise.de/newsticker/meldung/Maschinelles-Uebersetzen-Deutsches-Start-up-DeepL-will-230-Sprachkombinationen-unterstuetzen-3836533.html)
in the future, such as Chinese.
## Text Length Limit
According to the DeepL.com website, the length of the text that has to be translated is limited to 5000 characters.
Per default DeepLy will throw an exception if the length limit is exceeded.
You may call `$deepLy->setValidateTextLength(false)` to disable this validation.
## HTTP Client
Per default DeepLy uses a minimalistic HTTP client based on cURL. If you want to use a different HTTP client,
such as [Guzzle](https://github.com/guzzle/guzzle), create a class that implements the `HttpClient\HttpClientInterface`
and makes use of the methods of the alternative HTTP client. Then use `$deepLy->setHttpClient($yourHttpClient)`
to inject it.
> Note: If you experience issues with the integrated cURL client that could be solved by setting the
> `CURLOPT_SSL_VERIFYPEER` to `false`, first read this:
> [snippets.webaware.com.au/../](https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/)
>
> If it does not help try: `$deepLy->getHttpClient()->setSslVerifyPeer(false)`
### Guzzle
If you want to use Guzzle as the HTTP client: Support for Guzzle is available out-of-the-box.
Make sure you have installed Guzzle (preferably via Composer), then copy this code and paste it right after you instantiate DeepLy:
```php
$protocol = $deepLy->getProtocol();
$httpClient = new \ChrisKonnertz\DeepLy\HttpClient\GuzzleHttpClient($protocol);
$deepLy->setHttpClient($httpClient);
```
## Framework Integration
DeepLy comes with support for Laravel 5.x and since it also supports
[package auto-discovery](https://medium.com/@taylorotwell/package-auto-discovery-in-laravel-5-5-ea9e3ab20518)
it will be auto-detected in Laravel 5.5.
In Laravel 5.0-5.4 you manually have to register the service provider
`ChrisKonnertz\DeepLy\Integrations\Laravel\DeepLyServiceProvider` in the "providers" array and the facade
`ChrisKonnertz\DeepLy\Integrations\Laravel\DeepLyFacade` as an alias in the "aliases" array
in your `config/app.php` config file.
You can then access DeepLy like this: `$ping = \DeepLy::ping();`
## Demos
There are several demo scripts included in the `demos` folder:
* `demo_translate.php`: Demonstrates language translation. Write a text and the API will try to translate it into a language of your choice.
* `demo_detect.php`: Demonstrates language detection. Write a text and the API will tell you which language it thinks it is.
* `demo_split.php`: Demonstrates sentence detection. Write a text and the API will split it into sentences.
* `demo_ping.php`: Demonstrates DeepLy's `ping()` method by pinging the API.
## Request Limit
There is a request limit. The threshold of this limit is unknown.
## Internals
The "core" of this library consists of these classes:
* `DeepLy` - main class
* `HttpClient\CurlHttpClient` - HTTP client class
* `Protocol\JsonRpcProtocol` - JSON RPC is the protocol used by the DeepL API
* `ResponseBag\AbstractBag` - base wrapper class for the responses of the DeepL API
* `ResponseBag\SentencesBag` - concrete class for API responses to "split text" requests
* `ResponseBag\TranslationBag` - concrete class for API responses to "translate" requests
There are also some exception classes, interfaces, an alternative HTTP client implementation that uses Guzzle and classes for the Laravel integration.
## Current State
DeepL.com has officially released their API. They offer a premium service that includes access to this API.
However, it is still possible (at your own risk) to access the API without using the premium service.
Premium API access requires authentication with an API key. This will be supported by DeepLy 2.
## Disclaimer
This is not an official package. It is 100% open source and non-commercial.
The API of DeepL.com can be accessed without an API key but this might change in the future.
DeepL is a pr
没有合适的资源?快使用搜索试试~ 我知道了~
DeepLy是DeepLcom翻译API的一个PHP客户端
共18个文件
php:14个
md:1个
json:1个
需积分: 28 9 下载量 88 浏览量
2019-08-08
15:30:20
上传
评论
收藏 32KB ZIP 举报
温馨提示
DeepL.com是一个新的强大翻译服务。 它提供比其他流行的翻译引擎更好的翻译,可惜目前没有支持中文。 DeepLy是一个PHP包,实现客户端提供与DeepL进行交互的API。
资源推荐
资源详情
资源评论
收起资源包目录
DeepLy是DeepLcom翻译API的一个PHP客户端.zip (18个子文件)
chriskonnertz-DeepLy-200d0fb
src
ChrisKonnertz
DeepLy
HttpClient
GuzzleHttpClient.php 4KB
HttpClientInterface.php 1KB
CallException.php 162B
CurlHttpClient.php 7KB
Protocol
JsonRpcProtocol.php 7KB
ProtocolInterface.php 1KB
ProtocolException.php 194B
Integrations
Laravel
DeepLyFacade.php 321B
DeepLyServiceProvider.php 336B
DeepLy.php 20KB
ResponseBag
AbstractBag.php 2KB
TranslationBag.php 8KB
SentencesBag.php 3KB
BagException.php 182B
LICENSE 1KB
composer.lock 54KB
README.md 9KB
composer.json 948B
共 18 条
- 1
资源评论
weixin_39840515
- 粉丝: 446
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功