darrynten/google-cloud-translate-php
Composer 安装命令:
composer require darrynten/google-cloud-translate-php
包简介
PHP Google Cloud Translate Client with Extras
README 文档
README
An unofficial, fully unit tested Google Cloud Translate PHP client with extra sugar.
This is pretty much based on our Google Natural Language library and is quite similar.
PHP 7.0+
Why not just use the official one?
The official one is great, and we actually use it in this package, it just doesn't quite have the sort of features we needed, so we wrapped it up with some extra bits.
What extra features?
Cost Cutters
The Google Cloud Translate API costs money. If you're doing anything with it at scale, you're going to have to keep an eye on your calls to make sure things aren't running away with you. It's not cheap.
That's why we introduced some cost cutting features.
Caching Requests
By default this package caches your requests, something you would have to usually do yourself.
It caches using a framework-agnostic approach, whereby it leverages any host frameworks caching mechanism, and falls back to a temporary cache if there is no cache available.
The supported frameworks are detailed in the AnyCache project.
Examples include Laravel, Symfony, Doctrine, Psr6 and more.
This feature is on by default and can easily be disabled.
Cheapskate Mode
While not immediately clear, the Google Cloud Translate API charges per character. At the time of writing around $20 for 1m characters. (you pay for detection and translation at the same rates).
If you wish to first detect the language and then translate, you will pay twice for each character.
We're added cheapskate mode, and what this setting allows is for you to limit the amount of text used in language detection.
This feature is on by default and can easily be disabled. By default it
limits language detection to a tweet worth of characters, and you can
adjust the value too through the cheapskateCount property.
Additional Cost Saving Checks
If you submit a language that is not supported then you still get charged per character, so we ensure that we grab a copy of all supported languages and translation targets to make sure that you don't attempt to translate across unsupported languages, saving you money.
We also check to ensure you are not trying to translate the same target and source, which would also be expensive.
Conveniences
There are a few other conveniences like being able to set the target and source language, type, etc.
One use case would be running a single instance of text through multiple language attempts.
Usage
use DarrynTen\GoogleCloudTranslatePhp\GoogleCloudTranslate; // Config options $config = [ 'projectId' => 'my-awesome-project' // At the very least ]; // Make a translator $translate = new GoogleCloudTranslate($config); // Get information $translate->languages(); $translate->localizedLanguages(); // Detect languages $translate->detectLanguage($string); $translate->detectLanguageBatch([$strings]); // Translate $translate->translate($string); $translate->translateBatch([$strings]); // Set optional things $language->setType('html'); $language->setModel('base'); $language->setTargetLanguage('en'); $language->setSourceLanguage('es'); // Extra features $language->setCaching(false); $language->setCheapskate(false); $language->setCheapskateCount(50); // Full config options $config = [ 'projectId' => 'my-awesome-project', // required 'key' => 'api-key', // optional see note below 'target' => 'en', // optional default is en 'source' => 'en', // optional default is en 'model' => 'base', // optional 'type' => 'text', // optional 'authCache' => \CacheItemPoolInterface, // stores access tokens 'authCacheOptions' => $array, // cache config 'authHttpHandler' => callable(), // psr-7 auth handler 'httpHandler' => callable(), // psr-7 rest handler 'keyFile' => $json, // content 'keyFilePath' => $string, // path 'retries' => 3, // default is 3 'scopes' => $array, // app scopes 'cache' => $boolean, // cache 'cheapskate' => $boolean, // cheaper detection calls 'cheapskateCount' => 100, // how cheap? ]; // authCache, authCacheOptions, authHttpHandler and httpHandler are not // currently implemented.
See The Google Cloud Docs for more on these options and their usage.
Please note that while the Google Cloud Translation API supports authentication via service account and application default credentials like other Cloud Platform APIs, it also supports authentication via a public API access key.
If you wish to authenticate using an API key, follow the before you begin instructions to learn how to generate a key.
Options
setType($type)- Eitherhtml(default) ortextsetModel($model)setSourceLanguage($language)- Either ISO (en,es) or BCP-47 (en-ZA,en-GB).setTargetLanguage($language)- Either ISO or BCP-47.
If no language is provided then it is autodetected from the text and is returned with the response.
Missing Features
Feel free to open a PR!
Usage of Google\Cloud\Storage\StorageObject is presently not possible.
- Custom
authCacheandauthCacheOptions - Custom
httpHandlerandauthHttpHandler
Roadmap
- Implement missing features
Acknowledgements
- Open a PR and put yourself here :)
darrynten/google-cloud-translate-php 适用场景与选型建议
darrynten/google-cloud-translate-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.01k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 darrynten/google-cloud-translate-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 darrynten/google-cloud-translate-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-03-04