PHP code example of jonasyeah / translator

1. Go to this page and download the library: Download jonasyeah/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/ */

    

jonasyeah / translator example snippets


use Jonas\Translator\Translator;
use Jonas\Translator\Lng;

$config = [
	 	// HTTP 请求的超时时间(秒), 如果不设置, 系统默认使用5秒
        'timeout' => 5.0,

        // 默认发送配置, 如果不设置, 系统默认使用有道
        'default' => [
            // 默认可用的发送网关,
            'gateways' => [
                'youdao',
            ],
        ],
        // 可用的网关配置
        //如果没有设置qpp_key 和app_sec, 系统默认使用页面抓取活的接口
        'gateways' => [
            'baidu' => [
                'app_key' => '123xxxxx',
                'app_sec' => '123xxxxx',
            ],
            'baidu' => [
				'app_key' => '123xxxxx',
				'app_sec' => '123xxxxx',
            ],
        ],
]
$translator = new Translator($config);

//使用百度翻译
$res = $translator->baidu('天气不错');
//使用有道翻译
$res = $translator->youdao('天气不错');
//可以指定被翻译成的语言
$res = $translator->to(Lng::English)->baidu('天气不错');

//使用有道和百度翻译
$res = $translator->translate('天气不错', [Translator::Youdao, Translator::Baidu]);


//使用单一翻译
$res = $translator->baidu('天气不错');
//格式
[
	'gateway' => $gateway,
	'status' => self::STATUS_SUCCESS,
	'result' => ResultInstance,
];
//使用多种翻译
[
	"baidu"	=> [
		'gateway' => 'baidu',
		'status' => self::STATUS_SUCCESS, //状态
		'result' => ResultInstance,
    ],
    "youdao"	=> [
		'gateway' => 'youdao',
		'status' => self::STATUS_SUCCESS, //状态
		'result' => ResultInstance,
    ],
]
//ResultInstance 获取结果
//翻译结果
$result->getTransResult();
//词典结果, 如果翻译的是某个词
$result->getDictResult();
//原始结果
$result->getRaw()

//格式
[
	'gateway' => $gateway,
	'status' => self::STATUS_FAILURE,
	'exception' => $e,
];
//如果要查看原始异常
$e->raw