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;
}
|