正在显示
3 个修改的文件
包含
79 行增加
和
11 行删除
app/Console/Commands/Demo.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Console\Commands; | ||
| 4 | + | ||
| 5 | +use App\Fun; | ||
| 6 | +use Illuminate\Console\Command; | ||
| 7 | + | ||
| 8 | +class Demo extends Command | ||
| 9 | +{ | ||
| 10 | + /** | ||
| 11 | + * The name and signature of the console command. | ||
| 12 | + * | ||
| 13 | + * @var string | ||
| 14 | + */ | ||
| 15 | + protected $signature = 'demo'; | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * The console command description. | ||
| 19 | + * | ||
| 20 | + * @var string | ||
| 21 | + */ | ||
| 22 | + protected $description = 'Command description'; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Create a new command instance. | ||
| 26 | + * | ||
| 27 | + * @return void | ||
| 28 | + */ | ||
| 29 | + public function __construct() | ||
| 30 | + { | ||
| 31 | + parent::__construct(); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * Execute the console command. | ||
| 36 | + * | ||
| 37 | + * @return int | ||
| 38 | + */ | ||
| 39 | + public function handle() | ||
| 40 | + { | ||
| 41 | + echo Fun::translate("hello' word",'de'); | ||
| 42 | + } | ||
| 43 | +} |
| @@ -145,21 +145,45 @@ class Fun { | @@ -145,21 +145,45 @@ class Fun { | ||
| 145 | * @author:dc | 145 | * @author:dc |
| 146 | * @time 2022/11/16 17:43 | 146 | * @time 2022/11/16 17:43 |
| 147 | */ | 147 | */ |
| 148 | - private function translate($texts, $tls){ | ||
| 149 | - if (is_string($texts)) { | ||
| 150 | - $texts = [$texts]; | 148 | + public static function translate($texts, $tls){ |
| 149 | + | ||
| 150 | + static $langs = []; | ||
| 151 | + | ||
| 152 | + if(!empty($langs[$texts])){ | ||
| 153 | + return $langs[$texts]; | ||
| 151 | } | 154 | } |
| 152 | - if (is_string($tls)) { | ||
| 153 | - $tls = [$tls]; | 155 | + |
| 156 | + | ||
| 157 | + // 缓存的语言文件 | ||
| 158 | + $file = resource_path('lang/auto/'.$tls.'.php'); | ||
| 159 | + if(!is_file($file)){ | ||
| 160 | + @mkdir(dirname($file),0775,true); | ||
| 161 | + file_put_contents($file,"<?php".PHP_EOL); | ||
| 154 | } | 162 | } |
| 155 | - $data = [ | ||
| 156 | - 'texts' => $texts, | 163 | + |
| 164 | + require_once $file; | ||
| 165 | + | ||
| 166 | + // 已翻译过的 | ||
| 167 | + $langs = $_lang??[]; | ||
| 168 | + | ||
| 169 | + if(!empty($langs[$texts])){ | ||
| 170 | + return $langs[$texts]; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + $response = Http::post('https://translate.hbb618.cn/translates', [ | ||
| 174 | + 'texts' => [$texts], | ||
| 157 | 'sl' => 'en', | 175 | 'sl' => 'en', |
| 158 | - 'tls' => $tls, | ||
| 159 | - ]; | ||
| 160 | - $response = Http::post('https://translate.hbb618.cn/translates', $data); | 176 | + 'tls' => [$tls], |
| 177 | + ])->json(); | ||
| 178 | + | ||
| 179 | + $response = $response[0]['texts']??''; | ||
| 180 | + | ||
| 181 | + $langs[$texts] = $response; | ||
| 182 | + | ||
| 183 | + // 写入文件 | ||
| 184 | + file_put_contents($file,"\$_lang['".str_replace("'","\'",$texts)."'] = '".str_replace("'","\'",$response)."';".PHP_EOL,FILE_APPEND); | ||
| 161 | 185 | ||
| 162 | - return $response->json(); | 186 | + return $response; |
| 163 | } | 187 | } |
| 164 | 188 | ||
| 165 | } | 189 | } |
-
请 注册 或 登录 后发表评论