szy4211/translate

A translate SDK

1.0.0 2020-07-31 07:39 UTC

This package is auto-updated.

Last update: 2024-10-29 05:59:38 UTC


README

文本翻译 SDK.

Build Status StyleCI build status Latest Stable Version Latest Unstable Version Total Downloads License

安装

$ composer require szy4211/translate -vvv

使用

use Szy4211\Translate\Translate;

$config = [
    'default' => 'baidu', // 默认网关配置
    // 网关列表
    'gateways' => [
        'baidu' => [
            'app_id' => '',
            'app_secret' => '',
            'http_timeout' => 5.0, // 超时时间
            'http_options' => [],
        ],
        // ...
    ],
];

$translate = new Translate($config);
$transResult = $translate->translate('Hello');
echo $translate->getDstMessage(); // 你好

Laravel支持

  • 加载配置文件
php artisan vendor:publish --provider="Szy4211\Translate\TranslateServiceProvider"
  • 定义Env
TRANS_BAIDU_APP_ID='xxx'
TRANS_BAIDU_APP_SECRET='xxx'
  • 方法参数注入
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Szy4211\Translate\Translate;

class TranslateController extends Controller
{
    public function show(Request $request, Translate $translate, $query)
    {
        return $translate->translate($query);
    }
}

平台支持

License

MIT