charlemagne / shuwa
翻译包
Requires
- fabpot/goutte: ^3.2
This package is auto-updated.
Last update: 2024-09-19 22:12:57 UTC
README
免责声明:此项目仅用于教育目的。请考虑购买官方 Google Translate API。
v1.2.1 功能
- 实现请求超时以防止阻塞请求。
- 自动检测源语言。
- 从引用函数中检测语言。
- 修复错误。
注意:由于构造函数的编辑,此功能破坏了向后兼容性。尽管如此,它不会需要全新的版本,因此请阅读有关构造函数的新文档。
文档
需求
此项目需要
- php 7.*
- Composer
使用的包
- Fabpot \ Goutte
安装
运行以下命令来安装包
$ composer require charlemagne/shuwa
或者只需创建一个 composer.json 文件,如下所示
{ "require": { "charlemagne/shuwa": "*" } }
然后运行
$ composer update
使用
Shuwa 类
将 Composer 自动加载器引入到您的工作文件中,并使用 Shuwa 类
require '[PATH]/vendor/autoloader.php'; use charlemagne\Shuwa\Shuwa;
创建 Shuwa 对象
// will auto detect the source language and define the target language as 'en' $shuwa = new Shuwa(); // will auto detect the source language and define the target language as TARGET $shuwa = new Shuwa(TARGET); // replace SOURCE & TARGET with your source language and target language code $shuwa = new Shuwa(TARGET, SOURCE);
以下是一个包含所有 语言代码 的列表(请使用 ISO 639-1 代码版本)。
所有基本函数的快速列表
// return the source language code $shuwa->getSourceLang(); // return the target language code $shuwa->getTargetLang(); // set the source language code $shuwa->setSourceLang('en'); // set the target language code $shuwa->setTargetLang('it'); // set the safe mode $shuwa->setSafeMode(true/false); // set a proxy to the request $shuwa->setProxy("192.0.0.1:8080"); // get the current proxy $shuwa->getProxy(); // check if language code is valid $shuwa->checkLanguageCode('it'); // detect the language code from a quote $shuwa->detectLangFromQuote("Lorem Ipsum"); // translate a word or a quote $shuwa->translate('Hello world!'); // translate a word or a quote using a proxy $shuwa->translate('Hello world!', true);
FShuwa 类
如下使用 FShuwa 类
use charlemagne\Shuwa\FShuwa;
创建 FShuwa 对象
// will initialize ENGLISH => ITALIAN by default $fileShuwa = new FShuwa(); // replace SOURCE & TARGET with your source language and target language code $fileShuwa = new FShuwa(SOURCE, TARGET);
FShuwa 类扩展了 Shuwa,因此您基本上可以使用 Shuwa 的所有方法。还有一些方法可以帮助您翻译整个 Laravel\CodeIgniter 语言文件,但这可能需要几个小时。以下是 FShuwa 的所有方法列表
// returns true if quote is valid (See validation on Options) $fileShuwa->validate($quote); // replace ' to \' $fileShuwa->bind($quote); // translate a laravel Language file $fileShuwa->laravelTranslation('INPUT_FILE_PATH', 'OUTPUT_FILE_PATH'); // translate the whole $lang array from codeIgniter lang file $fileShuwa->codeIgniterTranslation($lang);
选项
您可以通过编辑 vendor\charlemagne\shuwa\src\config\shuwa.php 文件来修改 Shuwa 和 FShuwa 类的选项。让我们看看
Shuwa 类选项
在创建对象时启用安全模式。
'SAFE_MODE' => true,
将请求超时秒数设置为请求(防止死锁)。
'REQUEST_TIMEOUT' => 15,
FShuwa 类选项
如果您在翻译文件时想保留原始语言中的某个单词,应使用目标选项。例如 [EN -> IT]: 'I bought :number apples'; 如果您将目标设置为 ':', 则翻译后的引用将是: 'Ho comprato :number mele'; 但请记住,这可能会失败。如果这样,算法将返回未翻译的引用
'TARGET' => ':',
如果您想保留未翻译的 HTML 引用,请保持以下选项为 true。
'HTML_INTEGRITY' => true,
如果您想保留单词引用未翻译,请保持以下选项为 true。
'MANTAIN_SINGLE_WORDS' => true,
如果您想禁止某些单词,以便翻译不会影响包含它们的引用,请将那些单词添加到黑名单中
'BLACKLIST' => [ 'lorem', 'ipsum', 'docet'
]
安全模式
您有可能因为请求过多而暂时被 Google IP 封禁。为了 防止 和 解决 此问题,您可以使用安全模式。
我假设您不会使用我的包进行大量翻译,因此我默认禁用了安全模式,但您可以在 vendor/charlemagne/shuwa/src/config/shuwa.php 中修改默认设置,只需将 SAFE_MODE = true 设置即可。
这将使您的脚本速度减慢1分钟或更少,如果您想了解原因,请阅读《代理系统》部分。
您可以通过使用此函数在代码中设置安全模式。
$shuwa->setSafeMode(true);
当您被IP封禁时,Shuwa会自动运行安全模式来完成工作。
ProxySystem
ProxySys类可保护您免受Google IP封禁,但会大幅降低处理速度。如果您想快速翻译少量引用(少于20个),我建议您禁用SAFE_MODE。
使用
按照以下方式使用该类
use charlemagne\Shuwa\ProxySys; $system = new ProxySys();
以下是ProxySys的方法列表
// fill the list of proxyes $system->scrape(); // test proxyes and remove the slower ones. If list is empty, automatically call scrape() $system->filter(); // optimize the list. If it isn't filtered, automatically call filter() $system->optimize(); // fill the list in case the scrape() function doesn't works $system->supportSource(); // same but fill the list with https proxyes $system->supportSource(true); // reload the list $system->reload(); // return a fresh new proxy and remove it from the list $system->fire();
选项
您可以通过编辑vendor\charlemagne\shuwa\src\config\proxy.php
文件来修改ProxySys类的选项。让我们看一下
当Scrape()不工作时,在supportSource函数上启用https。
'RESERVE_SSL' => true,
对TEST_URL服务器发出的TEST请求的存活时间,我们需要设置一个最佳数字,以确保filter()函数只保留工作的代理
'TTL' => 1000,
代理列表维度限制
'LIST_LIMIT' => 100
当类被实例化时抓取代理列表
'AUTO_SCRAPE' => true,
当类被实例化时过滤代理列表
'AUTO_FILTER' => true,
当类被实例化时优化代理列表
'AUTO_OPTIMIZE' => true,
在重新加载时过滤代理列表
'FILTER_ON_RELOAD' => true,
在重新加载时优化代理列表
'OPTIMIZE_ON_RELOAD' => true,
打印proxySys操作,在通过终端执行时很有帮助
'DISPLAY' => [ 'SET' => true ]
捐赠
如果您欣赏我的工作并想请我喝杯咖啡,请随时这么做! :)
Paypal: https://www.paypal.me/charlemgn