基于 Laravel、Lumen 框架集成百度翻译、有道翻译、Google 翻译扩展包

栏目: PHP · 发布时间: 7年前

内容简介:Translate for laravel5.* or lumenInstalling

Translate for laravel5.* or lumen

Installing

$ composer require shaozeming/laravel-translate -v

Laravel

// config/app.php

    'providers' => [
        //...
        ShaoZeMing\LaravelTranslate\TranslateServiceProvider::class,    //This is default in laravel 5.5
    ],

And publish the config file:

$ php artisan vendor:publish --provider=ShaoZeMing\\LaravelTranslate\\TranslateServiceProvider

if you want to use facade mode, you can register a facade name what you want to use, for example translate :

// config/app.php

    'aliases' => [
        'Translate' => ShaoZeMing\LaravelTranslate\Facade\Translate::class,   //This is default in laravel 5.5
    ],

lumen

  • 在 bootstrap/app.php 中 82 行左右:
$app->register( ShaoZeMing\LaravelTranslate\TranslateServiceProvider::class);

vendor/ShaoZeMing/laravel-translate/config/translate.php 拷贝到项目根目录 /config 目录下,并将文件名改成 translate.php

configuration

// config/translate.php

     //使用什么翻译驱动
       // 目前支持这几种: "baidu", "youdao","google"
       /*
        *  默认使用google  google使用的是免费接口爬取,目前能用,为了确保稳定,请配置一个备用服务,
        */
       'defaults' => [
           'driver' => 'google',   //默认使用google翻译
           'spare_driver' => 'baidu',  // 备用翻译api ,第一个翻译失败情况下,调用备用翻译服务,填写备用翻译api 需要在下面对应的drivers中配置你参数
           'from' => 'zh',   //原文本语言类型 ,目前支持:auto【自动检测】,en【英语】,zh【中文】,jp【日语】,ko【韩语】,fr【法语】,ru【俄文】,pt【西班牙】
           'to' => 'en',     //翻译文本 :en【英语】,zh【中文】,jp【日语】,ko【韩语】,fr【法语】,ru【俄文】,pt【西班牙】
       ],


       'drivers' => [
           'baidu' => [
               'base_url' => 'http://api.fanyi.baidu.com/api/trans/vip/translate',
               //App id of the translation api
               'app_id' => '20180611000174972',
               //secret of the translation api
               'app_key' => 'cEXha7w4elaXO23NJ2Tt',
           ],

           'youdao' => [
               'base_url' => 'https://openapi.youdao.com/api',
               //App id of the translation api
               'app_id' => '',
               //secret of the translation api
               'app_key' => '',
           ],

           'google' => [
               'base_url' => 'http://translate.google.cn/translate_a/single',
               'app_id' => '',
               'app_key' => '',
           ],
       ],

Usage

use Translate;

//第1种
$result = Translate::translate('你知道我对你不仅仅是喜欢');
print_r($result);

Example:

// 更换翻译服务商
$result = Translate::setDriver('baidu')->translate('你知道我对你不仅仅是喜欢');
print_r($result);

// 更换翻译语言 可选语言请看配置文件中可定义的几种
$from="en";
$to="zh";
$result = Translate::setFromAndTo($from,$to)->translate('I love you.');
print_r($result);

License

MIT


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

我在阿里做运营

我在阿里做运营

芮曦(@小马鱼) / 电子工业出版社 / 2018-7 / 59.00元

《我在阿里做运营》是一本散发着浓浓阿里味儿的运营书。作者进入互联网行业7年,曾就职于携程、阿里巴巴等大平台,也服务过小微企业、传统企业及诸多职场新人。不仅经历过各类运营岗,也经历过市场、品牌等岗位,对精细化运营、数据化运营和低成本运营有着深刻见解。 本书展示了在阿里这样的大平台做运营工作的真实场景,也提炼了适用于小微企业的经验,以及让运营新人快速上手的技能和自我修养、职业规划。一起来看看 《我在阿里做运营》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具