tacman/libretranslate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tacman/libretranslate

Composer 安装命令:

composer require tacman/libretranslate

包简介

PHP interface for the open source LibreTranslate project

README 文档

README

PHP Interface to the open source LibreTranslate project

Install

Composer (recommended)

composer require jefs42/libretranslate

Local Development

composer config repositories.libretranslate '{"type": "vcs", "url": "git@github.com:survos/SurvosGridBundle.git"}'

composer config repositories.libretranslate '{"type": "path", "url": "~/g/tacman/libretranslate"}'
composer require tacman/libretranslate:"*@dev"
require('vendor/autoload.php');

use Jefs42\LibreTranslate;

$translator = new LibreTranslate();

Manual

Download the LibreTranslate.php file from src/ directory and place somewhere in you project.

Include the file in your project:

require_once("path/to/LibreTranslate.php");

use Jefs42\LibreTranslate;

$translator = new LibreTranslate();

Usage

By default, this class will try to connect to and use a local installation of the LibreTranslate server at http://localhost:5000

Pass host and optionally port, source and/or target parameters to override defaults.

// use locally installed LibreTranslate server on port 5000 with default language settings
$translator = new LibreTranslate();

// specify server with alternate port
$translator = new LibreTranslate("https://libretranslate", 5042);

// use localhost, default port, but override default languages used in translations
$translator = new LibreTranslate(null, null, 'de', 'it');

LibreTranslate Mirros

Set API Key

Depending on the server settings, you may need to provide a valid API key to make translation request.

$translator->setApiKey('xxxxx-xxxxx-xxxxx');

Set Default Languages

The default source and target languages are set by the server. You may override these settings when constructing the class, or as needed:

// Set to translate from English to Swedish
$translator->setLanguages('en', 'sv');

// change only one - source or target
$translator->setSource('es');

$translator->setTarget('ru');

Each translation function also supports specifying the languages to use when calling the function.

Detect Language

LibreTranslate will attempt to determine the language of the string passed to it.

$lang = $translator->detect("mi nombre es jefs42");
// expected result: $lang = 'es'

Translate Text

Translate a string of text, or an array of multiple texts. A server may or may not have a character limit set. For larger texts see Translate File.

// translate text using current default source/target languages
$translatedText = $translator->translate("My name is jefs42");

// specifally request languages to use in translation.
// eg. from English to German
$translatedText = $translator->translate("My name is jefs42", "en", "de");

// translate multiple texts in one call
// returns array of translated texts
$translatedText = $translator->translate(["My name is jefs42", "Where is the bathroom?"]);

Translate File

Translate a file of text.

Check $translator->Settings(), current supported formats appear to be - .txt, .odt, .odp, .docx and .pptx

// translate file using current default source/target languages
$translatedText = $translator->translateFile("/full/path/to/file.txt");

// translate file with specific source/target languages
// eg. from English to Italian
$translatedText = $translator->translateFile("/full/path/to/file.txt", "en", "it");
  • Translation server may have max size limits on post/files size.
  • Could pass $_SERVER['FILES']['formfield']['tmp_name'] if using for a web form.

Suggest

Submit a suggested translation to the server.

// Submit suggestion using current source/target language
$translator->suggest('My name is jefs42', 'Mi nombre es jefs42');

// Specify languages for suggestion
$translator->suggest('My name is jefs42', 'Mi chiamo jefs42', 'en', 'it');

Sugesstions must be enabled on the LibreTranslate server.

Get Available Languages

Get the list of languages available on the current server.

$languages = $translator->Languages();
/*
Returns array of language codes/names:
[en] => 'English',
[it] => 'Italian',
...
*/

Check Server Settings

Get settings current server is running with.

$settings = $translator->Settings();
/*
Returns array of settings and their current values:
[api_keys] => 1,
[keyRequired] => ,
[char_limit] => -1,
...
*/

See LibreTranslate Arguments for server settings.

LTManage

If you are running a locally hosted LibreTranslate server, you may have access to ltmanage. This allows you to view current keys and their request limits, as well as create new keys and delete current keys.

See LibreTranslate Manage Keys for details.

If ltmanage is found in the local path then the following additonal functions will be available for use:

listKeys

Will return an array of current keys and their request limits.

$keys = $translator->listKeys();
/*
Returns array of current keys and limits:
[
    'key1' => '500',
    'key2' => '50',
    ...
]
*/

addKey

Create a new key for local server with optional request limit.

// create a new key limited to server defaults
$key = $translator->addKey();

// create a new key with a specific request limit (higher or lower than server default)
$key = $translator->addKey(400); // limit to 400 requests per minute

/*
Returns string of new key:
"xxxxx-xxxx-xxxx"
*/

removeKey

Delete an existing key from the available keys.

try {
    $translator->removeKey("xxxxx-xxxxx-xxxxx");
} catch (Exception $e) {
    // key doesn't exist
}
/*
Returns true on deletion
*/

tacman/libretranslate 适用场景与选型建议

tacman/libretranslate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 422 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「translate」 「libretranslate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tacman/libretranslate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tacman/libretranslate 我们能提供哪些服务?
定制开发 / 二次开发

基于 tacman/libretranslate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 422
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-01-19