本篇文章为大家展示了如何在Laravel框架中调用百度翻译API,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
static function translate($_query,$_src_lang,$_desc_lang){ #Trans:就是百度提供的PHP类 $_res = Trans::translate($_query,$_src_lang,$_desc_lang); $_result = @$_res["trans_result"][0]["dst"]; return $_result; } #google 和 百度的不同.. const LANG=[ "de" => "de", "en" => "en", "es" => "spa", "fr" => "fra", "it" => "it", "ja" => "jp", "ko" => "kor", "pt" => "pt", "ru" => "ru", "th" => "th", "vi" => "vie", "zh-CN" => "zh", "zh-TW" => "cht", "id" => "en", "ms" => "en", "my" => "en", ]; static function startTrans($_trans_name='down.php',$_lang_name='zh-CN'){ $_lang_dir = Storage::disk('lang'); $_dirs = $_lang_dir->directories(); $_transArr = self::_getSrcTransArr($_lang_name,$_trans_name); foreach($_dirs as $_lang){ #当然,要跳过指定的文件 if($_lang == $_lang_name){ continue; } $_tran_path = "$_lang/{$_trans_name}"; if(!$_lang_dir->exists($_tran_path)){ #这里直接touch就行了 $_lang_dir->copy("template.php",$_tran_path); } $_trans_file_arr = []; #下面是翻译工作 foreach($_transArr as $key=>$_row){ $_trans_file_arr[$key] = self::translate($_row,self::LANG[$_lang_name],self::LANG[$_lang]); } #将数组写入文件 $str=''.PHP_EOL.'return ' . var_export($_trans_file_arr,true).";"; file_put_contents(resource_path("lang/{$_tran_path}"), $str); } } #读取指定文件数组 private static function _getSrcTransArr($_dir_name,$_trans_name){ $_src_file = Storage::disk('lang')->files($_dir_name); $_out_data = []; foreach($_src_file as $_file){ if(stripos($_file,$_trans_name)!==false){ $_out_data = include_once (resource_path("lang/{$_file}")); break; } } return $_out_data; }
上述内容就是如何在Laravel框架中调用百度翻译API,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。