PHP code example of her-cat / baidu-translator

1. Go to this page and download the library: Download her-cat/baidu-translator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

her-cat / baidu-translator example snippets


use HerCat\BaiduTranslator\BaiduTranslator;

$appId = 'APP ID';
$key = '密钥';

$translator = new BaiduTranslator($appId, $key);

$text = 'hello';    // 需要翻译的内容
$to = 'zh';         // 译文语言
$from = 'auto';     // 翻译源语言,`auto` 表示自动获取
$format = 'json';   // 格式化结果

$result = $translator->translate($text, $to, $from, $format);

return [
    'app_id' => env('BAIDU_TRANSLATOR_APP_ID'),
    'key' => env('BAIDU_TRANSLATOR_KEY'),
];

.
.
.
public function show(BaiduTranslator $translator) 
{
    $response = $translator->translate('hello');
}
.
.
.

.
.
.
public function show() 
{
    $response = app('baiduTranslator')->translate('hello');
}
.
.
.