oss-tools/laravel-libretranslate 问题修复 & 功能扩展

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

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

oss-tools/laravel-libretranslate

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固