malvik-lab/libre-translate-api-client
Composer 安装命令:
composer require malvik-lab/libre-translate-api-client
包简介
A PHP client for interacting with LibreTranslate, a free and open-source translation API.
关键字:
README 文档
README
An API client written in PHP to interact with LibreTranslate, a free, open-source translation API.
Characteristics
- Translations between different languages supported by LibreTranslate.
- Automatic language detection.
- Support for API Key, if needed.
- Easy setup and use.
Installation
You can install the client via Composer:
composer require malvik-lab/libre-translate-api-client
Initializing
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client as HttpClient; use MalvikLab\LibreTranslateClient\Client; // LibreTranslate server URL $baseUrl = 'http://localhost:5000'; // If the server requires an API Key, enter it here. // If the server is free (no need for API Key), pass `null` as a parameter. $apiKey = null; // API Key (optional) // Example 1: Minimal configuration (without API Key, if the server does not require it) $client = new Client($baseUrl); // No API Key required // Example 2: Configuration with API Key (if required by the server) $client = new Client($baseUrl, $apiKey); // Pass the API Key (can be null) // Example 3: Configuration with a custom Guzzle instance (for advanced customization) $httpClient = new HttpClient(); // Custom Guzzle client (useful for adding middlewares, timeouts, etc.) $client = new Client($baseUrl, $apiKey, $httpClient); // With API Key // or $client = new Client($baseUrl, null, $httpClient); // Without API Key (if server is free)
Client Methods
Usage
Detect
Implementation
<?php // ... use MalvikLab\LibreTranslateClient\DTO\DetectRequestDTO; $request = new DetectRequestDTO('Il diavolo fa le pentole ma non i coperchi.'); $response = $client->detect($request);
Output
MalvikLab\LibreTranslateClient\DTO\DetectResponseDTO Object ( [items] => Array ( [0] => MalvikLab\LibreTranslateClient\DTO\DetectDTO Object ( [confidence] => 100 [language] => it ) ) )
Languages
Implementation
<?php // ... $response = $client->languages();
Output
MalvikLab\LibreTranslateClient\DTO\LanguagesResponseDTO Object ( [items] => Array ( [0] => MalvikLab\LibreTranslateClient\DTO\LanguageDTO Object ( [code] => en [name] => English [targets] => Array ( [0] => ar // ... [45] => zt ) ) // ... ) )
Translate
Implementation
<?php // ... use MalvikLab\LibreTranslateClient\DTO\TranslateRequestDTO; use MalvikLab\LibreTranslateClient\Enum\FormatEnum; $request = new TranslateRequestDTO( 'Il lupo perde il pelo ma non il vizio', 'it', 'en', FormatEnum::TEXT, 3 ); $response = $client->translate($request);
Output
MalvikLab\LibreTranslateClient\DTO\TranslateResponseDTO Object ( [translatedText] => The wolf loses the fur but not the vice [alternatives] => Array ( [0] => The wolf loses his hair but not his vice [1] => The wolf loses his fur but not his vice [2] => The wolf loses his fur but not the vice ) )
Translate file
Implementation
<?php // ... use MalvikLab\LibreTranslateClient\DTO\TranslateFileRequestDTO; $request = new TranslateFileRequestDTO( 'path/to/file.txt', 'it', 'en' ); $response = $client->translateFile($request);
Output
MalvikLab\LibreTranslateClient\DTO\TranslateFileResponseDTO Object ( [translatedFileUrl] => http://localhost:5000/download_file/72e720fe-1568-457a-a1a4-017939c9f533.file_en.txt )
Suggest
Implementation
<?php // ... use MalvikLab\LibreTranslateClient\DTO\SuggestRequestDTO; $request = new SuggestRequestDTO( 'Hello world!', '¡Hola mundo!', 'en', 'es' ); $response = $client->suggest($request);
Output
MalvikLab\LibreTranslateClient\DTO\SuggestResponseDTO Object ( [success] => 1 )
Frontend Settings
Implementation
<?php // ... $response = $client->frontendSettings();
Output
MalvikLab\LibreTranslateClient\DTO\FrontendSettingsResponseDTO Object ( [apiKeys] => [charLimit] => -1 [filesTranslation] => 1 [frontendTimeout] => 500 [keyRequired] => [language] => MalvikLab\LibreTranslateClient\DTO\SettingsLanguageDTO Object ( [source] => MalvikLab\LibreTranslateClient\DTO\SourceDTO Object ( [code] => auto [name] => Auto Detect ) [target] => MalvikLab\LibreTranslateClient\DTO\TargetDTO Object ( [code] => sq [name] => Albanian ) ) [suggestions] => 1 [supportedFilesFormat] => Array ( [0] => .txt [1] => .odt [2] => .odp [3] => .docx [4] => .pptx [5] => .epub [6] => .html ) )
Running Test
Without API Key
BASE_URL=http://localhost:5000 vendor/bin/phpunit tests --testdox
With API Key
BASE_URL=http://localhost:5000 API_KEY=yourApiKey vendor/bin/phpunit tests --testdox
malvik-lab/libre-translate-api-client 适用场景与选型建议
malvik-lab/libre-translate-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「translation」 「API-Client」 「open-source」 「language-detection」 「libretranslate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 malvik-lab/libre-translate-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 malvik-lab/libre-translate-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 malvik-lab/libre-translate-api-client 相关的其它包
同方向 / 同关键字的高下载量 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
Skolkovo API Client
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Symfony localise.biz library
This bundle provides translations for entities in your project. It's a bundle, but can be used standalone.
统计信息
- 总下载量: 50
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-21