chriskonnertz/deeply
Composer 安装命令:
composer require chriskonnertz/deeply
包简介
DeepLy is a PHP client for the DeepL.com translation API
README 文档
README
DeepLy 2
DeepL is a next-generation translation service. DeepLy is a dependency-free PHP library that implements a client to interact with the DeepL API using an API key. You can get an API key for free on their website. DeepLy automatically supports both the free and the pro API. For interactive demo scripts take a look at the demos folder.
Installation
This library requires PHP 8.0 or higher and the cURL extension. Install DeepLy trough Composer:
composer require chriskonnertz/deeply
Examples
$deepLy = new ChrisKonnertz\DeepLy\DeepLy('your-api-key'); $translatedText = $deepLy->translate('Hello world!', 'DE'); echo $translatedText; // Prints "Hallo Welt!"
💡 An interactive PHP demo script is included. It is located at demos/demo_translate.php.
Sophisticated Example
$deepLy = new ChrisKonnertz\DeepLy\DeepLy('your-api-key'); 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. The exception will have an explanatory message and a specific error code.
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).
If you need to specify advanced settings, use the setSettings() method: $deepLy->setSettings($glossaryId);
Auto-Detect Language
⚠️ ATTENTION: Using this method increases the usage statistics of your account!
DeepLy has a method that uses the DeepL API to detect the language of a text:
$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 English 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.
Supported Languages
DeepL(y) supports these languages:
| Code | Language | Code | Language | |
|---|---|---|---|---|
| auto | Auto detect | KO | Korean | |
| ID | Indonesian | TR | Turkish | |
| IT | Italian | ZH | Chinese | |
| BG | Bulgarian | LT | Lithuanian | |
| CS | Czech | LV | Latvian | |
| DA | Danish | NB | Norwegian | |
| DE | German | NL | Dutch | |
| EL | Greek | PL | Polish | |
| EN | English | PT | Portuguese | |
| ES | Spanish | RO | Romanian | |
| ET | Estonian | RU | Russian | |
| FI | Finnish | SK | Slovak | |
| PT | French | SL | Slovenian | |
| HU | Hungarian | SV | Swedish | |
| JA | Japanese |
💡 Note that only the source language can be auto-detected.
Glossaries
To get a list with information about all your glossaries, do:
$glossaries = $deepLy->getGlossaries(); print_r($glossaries); // Prints an array with Glossary objects
Output:
Array
(
[0] => ChrisKonnertz\DeepLy\Models\Glossary Object
(
[glossaryId] => 56cab399-ac8e-4a57-aadc-fa95103f2de5
[entryCount] => 2
...
)
[2] => ChrisKonnertz\DeepLy\Models\Glossary Object
(
[glossaryId] => d9eb53b5-3929-49a1-b5e1-df1eb8be93c9
[entryCount] => 5
...
)
)
To get information about a specific glossary, do:
$glossary = $deepLy->getGlossary('your-glossary-id'); print_r($glossary); // Prints a \stdClass
Output:
ChrisKonnertz\DeepLy\Models\Glossary Object
(
[glossaryId] => d9eb53b5-3929-49a1-b5e1-df1eb8be93c9
[name] => DeepLy Test
[ready] => 1
[from] => en
[to] => de
[creationTimeIso] => 2022-04-21T17:46:31.83913+00:00
[creationDateTime] => DateTime Object
[entryCount] => 2
)
To get the translation entries of a specific glossary, do:
$entries = $deepLy->getGlossaryEntries('your-glossary-id'); print_r($entries); // Prints an array with string items
Output:
Array
(
[Entry 1 DE] => Entry 1 EN
[Entry 2 DE] => Entry 2 EN
)
To create a new glossary with translation entries, do:
$glossary = $deepLy->createGlossary('test', 'de', 'en', ['Example DE' => 'Example EN']);
To delete an existing glossary, do:
$deepLy->deleteGlossary('your-glossary-id');
💡 An interactive PHP demo script is included. It is located at demos/demo_glossaries.php.
Documents
Translating documents consists of three steps. The first step is to upload a document:
$filename = __DIR__.'/test_document_original.pdf'; $result = $deepLy->uploadDocument($filename, 'DE'); var_dump($result);
Output:
ChrisKonnertz\DeepLy\Models\DocumentHandle Object
(
[documentId] => D014F316B7A173079074BE76F530F846
[documentKey] => 39FF8B10D20621096F23BF96CC103E12074727007C62963CF49AE8A9965D7695
)
💡 The maximum upload limit for any document is 10 MB and 1.000.000 characters.
⚡ Every file upload is at least billed with 50.000 characters!
The second step is to wait for the DeepL.com API to finish processing (translating) the document. You can check the state:
$result = $deepLy->getDocumentState($result->documentId, $result->documentKey); var_dump($result);
Output:
ChrisKonnertz\DeepLy\Models\DocumentState Object
(
[documentId] => D014F316B7A173079074BE76F530F846
[status] => done
[billedCharacters] => 50000
[secondsRemaining] => null
)
In this case the document has been processed. This is indicated by "status" being "done" and "seconds_remaining" being null.
💡 The document life cycle is: queued ➜ translating ➜ done (or error)
There are constants that you can use to check these values:
DocumentState\STATUS_DONEetc.
The third step is to download the document:
$deepLy->downloadDocument($documentId, $documentKey, 'test_document_translated.pdf');
If you do not want to store the file, do:
$contents = $deepLy->downloadDocument($documentId, $documentKey);
⚡ A document can only be downloaded once!
💡 An interactive PHP demo script is included. It is located at demos/demo_documents.php.
Usage Statistic
To get usage statistics, do:
$usage = $deepLy->usage(); // Returns an object of type "Usage" echo $usage->characterCount.'/'.$usage->characterLimit . ' characters ('.round($usage->characterQuota * 100).'%)';
Depending on the user account type, some usage types will be null. Learn more: https://www.deepl.com/de/docs-api/other-functions/monitoring-usage/
Framework Integration
DeepLy comes with support for Laravel 5.5+ and since it also supports
package auto-discovery
it will be auto-detected. However, you have to store your DeepL API key manually in the .env file, like this:
DEEPL_API_KEY = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Afterwards you can access DeepLy like this: $ping = \DeepLy::ping();
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, 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_VERIFYPEERtofalse, first read this: snippets.webaware.com.au/../If it does not help try:
$deepLy->getHttpClient()->setSslVerifyPeer(false)
💡 You can set up a proxy with:
$deepLy->getHttpClient()->setProxy('ip:port', 'user:password')
Tests
Export your API key:
export DEEPL_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Run composer install from the DeepLy directory, then run the tests:
./vendor/phpunit/phpunit/phpunit
Differences to V1
To upgrade from v1 to v2, make sure you specify the API key when instantiating the DeepLy object. Apart from the changes mentioned above your v1 code should still work with v2 as long as you did not write your own HTTP client or extended the DeepLy class with a custom class. To learn more about the changes, please take a look at the changelog.
Disclaimer
This is not an official package. It is 100% open source and non-commercial.
DeepL is a product of DeepL GmbH. More info: deepl.com/publisher.html
Notes
-
Texts have to be UTF8-encoded.
-
If you are looking for a real-world example application that uses DeepLy, you may take a look at Translation Factory.
-
The code of this library is formatted according to the code style defined by the
PSR-2 standard. -
Status of this repository: Maintained. Create an issue and you will get a response, usually within 48 hours.
chriskonnertz/deeply 适用场景与选型建议
chriskonnertz/deeply 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124.73k 次下载、GitHub Stars 达 229, 最近一次更新时间为 2017 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translation」 「api」 「client」 「library」 「language」 「translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chriskonnertz/deeply 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chriskonnertz/deeply 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chriskonnertz/deeply 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
A PSR-7 compatible library for making CRUD API endpoints
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 124.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 229
- 点击次数: 30
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-31