Download the PHP package mantoufan/yzhantranslator without Composer
On this page you can find all versions of the php package mantoufan/yzhantranslator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mantoufan/yzhantranslator
More information about mantoufan/yzhantranslator
Files in mantoufan/yzhantranslator
Package yzhantranslator
Short Description Language translater using OpenAI with cache. 使用 AI 翻译内容到不同语言,缓存结果以节省费用.
License MIT
Informations about the package yzhantranslator
YZhanTranslator
YZhanTranslator is a PHP library for translating content using OpenAI's language models with caching capabilities. It supports translating strings, JSON objects and images arrays, as well as language detection.
YZhanTranslator can automatically cache (configurable) results to save costs.
Features
- Translate strings and JSON objects
- Detect language of input text (including JSON content)
- Translate images by providing URLs and receiving descriptions
- Cache translation results to save costs
- Customizable prompts for fine-tuned translations
- Built on top of YZhanGateway for flexible API interactions
Requirements
- PHP 5.4 or higher
- OpenAI API key
- Composer for dependency management
Installation
Install YZhanTranslator using Composer:
Usage
Basic Translation
JSON Translation
Language Detection
Language JSON Detection
Custom Prompts
Images Descriptions
You can now translate images by providing an array of image URLs.
The translator will return descriptions for each image in the specified language.
Configuration
YZhanTranslator uses environment variables for configuration. Create a .env
file in your project root with the following contents:
Parameters
The translate()
and detect()
methods accept an optional $params
array for additional configuration:
prompt
: (string) Additional prompt to customize results, e.g., 'do not translate the href attribute'cache
: (array) Caching optionstype
: (string) Cache type (default is 'File')params
: (array) Cache-specific parametersdir
: (string) Directory for file-based cachingmaxAge
: (int) Cache expiration time in seconds
timeout
: (int) Request timeout in seconds (default is 6 seconds)
Example usage with parameters:
Testing
Run the test suite using PHPUnit:
Generate a code coverage report:
License
This project is open-sourced software licensed under the MIT license.
Author
Shon Wu - GitHub
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.