定制 oss-tools/laravel-libretranslate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

oss-tools/laravel-libretranslate

最新稳定版本:v2.0.0

Composer 安装命令:

composer require oss-tools/laravel-libretranslate

包简介

Package to introduce a Client for translating text using LibreTranslate in Laravel.

README 文档

README

Latest Version Software License GitHub Workflow Status Check & fix styling Total Downloads

This package adds a client to translate text in Laravel using LibreTranslate.

What is LibreTranslate?

LibreTranslate is a free and open source translation library.

Installation

You can install the package via composer:

composer require oss-tools/laravel-libretranslate

Configuration

To set up the package, you will need to set the below env variables.

LIBRETRANSLATE_HOST=https://mylibretranslateserver.com
LIBRETRANSLATE_API_KEY=your-api-key
LIBRETRANSLATE_DEFAULT_SOURCE=en

Note: The default value for LIBRETRANSLATE_HOST is set to https://translate.argosopentech.com however, we recommend setting up your own server or using a host that is suitable for your needs for production.

Usage

use OSSTools\LibreTranslate\Client;
use OSSTools\LibreTranslate\Translation\LanguageCodes;

class ExampleController extends Controller
{
    public function translate()
    {
        $client = new Client();
        
        // Returns an instance of \OSSTools\LibreTranslate\Translation\TranslationCollection
        $result = $client->translate('This is some text', LanguageCodes::SPANISH);
        
        // Returns an array of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate('This is some text', LanguageCodes::SPANISH)->getAll();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->first();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->last();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationItem
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->get('A test');
        
        // Returns "Una prueba"
        $result = $client->translate(['This is some text', 'A test'], LanguageCodes::SPANISH)->last()->getText();
    }
}

Detecting a language from some text

use OSSTools\LibreTranslate\Client;

class ExampleController extends Controller
{
    public function translate()
    {
        $client = new Client();
        
        // Returns an instance of \OSSTools\LibreTranslate\Translation\TranslationDetectionCollection
        $result = $client->detect('This is some text');
        
        // Returns an array of \OSSTools\LibreTranslate\Translation\TranslationDetectionItem
        $result = $client->detect('This is some text')->getAll();
        
        // Returns a single instance of \OSSTools\LibreTranslate\Translation\TranslationDetectionItem
        $result = $client->translate('This is some text')->first();

        // Returns "en"
        $result = $client->detect('Some text')->first()->getLanguage();
        
        // Returns "es"
        $result = $client->detect('Una prueba')->first()->getLanguage();
    }
}

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-20