|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Translate;
|
|
|
|
|
|
|
|
use App\Helper\Translate;
|
|
|
|
use App\Models\News\News;
|
|
|
|
use App\Models\News\NewsCategory;
|
|
|
|
use App\Models\Product\Category;
|
|
|
|
use App\Models\Product\Detail;
|
|
|
|
use App\Models\Product\Keyword;
|
|
|
|
use App\Models\Product\Product;
|
|
|
|
use App\Services\ProjectServer;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class SiteTranslate extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'site_translate {project_id} {tls}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = '站点产品,新闻,博客整站翻译';
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
//项目id
|
|
|
|
$project_id = $this->argument('project_id');
|
|
|
|
//翻译目标语种
|
|
|
|
$tls = $this->argument('tls');
|
|
|
|
|
|
|
|
//值为空的替换符
|
|
|
|
$replace = '!@#$%';
|
|
|
|
|
|
|
|
ProjectServer::useProject($project_id);
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 整站内容翻译开始 , 翻译语种 : ' . $tls);
|
|
|
|
|
|
|
|
//翻译新闻分类
|
|
|
|
$news_category = NewsCategory::select(['id', 'name', 'remark', 'seo_title', 'seo_des', 'seo_keywords'])->get();
|
|
|
|
foreach ($news_category as $v_news_cate) {
|
|
|
|
$need_trans_news_cate = [
|
|
|
|
$v_news_cate->name,
|
|
|
|
$v_news_cate->remark ?: $replace,
|
|
|
|
$v_news_cate->seo_title ?: $replace,
|
|
|
|
$v_news_cate->seo_des ?: $replace,
|
|
|
|
$v_news_cate->seo_keywords ?: $replace
|
|
|
|
];
|
|
|
|
$re_trans_news_cate = Translate::translate($need_trans_news_cate, $tls);
|
|
|
|
if (isset($re_trans_news_cate[0]['code']) && $re_trans_news_cate[0]['code'] == 200) {
|
|
|
|
$news_cate_trans_texts = $re_trans_news_cate[0]['texts'];
|
|
|
|
$v_news_cate->name = $news_cate_trans_texts[0] != $replace ? $news_cate_trans_texts[0] : '';
|
|
|
|
$v_news_cate->remark = $news_cate_trans_texts[1] != $replace ? $news_cate_trans_texts[1] : '';
|
|
|
|
$v_news_cate->seo_title = $news_cate_trans_texts[2] != $replace ? $news_cate_trans_texts[2] : '';
|
|
|
|
$v_news_cate->seo_des = $news_cate_trans_texts[3] != $replace ? $news_cate_trans_texts[3] : '';
|
|
|
|
$v_news_cate->seo_keywords = $news_cate_trans_texts[4] != $replace ? $news_cate_trans_texts[4] : '';
|
|
|
|
$v_news_cate->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 新闻分类翻译完成');
|
|
|
|
|
|
|
|
//翻译新闻
|
|
|
|
$news = News::select(['id', 'name', 'text', 'remark', 'seo_title', 'seo_description', 'seo_keywords'])->get();
|
|
|
|
foreach ($news as $v_news) {
|
|
|
|
$need_trans_news = [
|
|
|
|
$v_news->name,
|
|
|
|
$v_news->text ?: $replace,
|
|
|
|
$v_news->remark ?: $replace,
|
|
|
|
$v_news->seo_title ?: $replace,
|
|
|
|
$v_news->seo_description ?: $replace,
|
|
|
|
$v_news->seo_keywords ?: $replace
|
|
|
|
];
|
|
|
|
$re_trans_news = Translate::translate($need_trans_news, $tls);
|
|
|
|
if (isset($re_trans_news[0]['code']) && $re_trans_news[0]['code'] == 200) {
|
|
|
|
$news_trans_texts = $re_trans_news[0]['texts'];
|
|
|
|
$v_news->name = $news_trans_texts[0] != $replace ? $news_trans_texts[0] : '';
|
|
|
|
$v_news->text = $news_trans_texts[1] != $replace ? $news_trans_texts[1] : '';
|
|
|
|
$v_news->remark = $news_trans_texts[2] != $replace ? $news_trans_texts[2] : '';
|
|
|
|
$v_news->seo_title = $news_trans_texts[3] != $replace ? $news_trans_texts[3] : '';
|
|
|
|
$v_news->seo_description = $news_trans_texts[4] != $replace ? $news_trans_texts[4] : '';
|
|
|
|
$v_news->seo_keywords = $news_trans_texts[5] != $replace ? $news_trans_texts[5] : '';
|
|
|
|
$v_news->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 新闻翻译完成');
|
|
|
|
|
|
|
|
//翻译产品分类
|
|
|
|
$product_category = Category::select(['id', 'title', 'keywords', 'describe', 'seo_title', 'seo_des', 'seo_keywords'])->get();
|
|
|
|
foreach ($product_category as $v_product_cate) {
|
|
|
|
$need_trans_product_cate = [
|
|
|
|
$v_product_cate->title,
|
|
|
|
$v_product_cate->keywords ?: $replace,
|
|
|
|
$v_product_cate->describe ?: $replace,
|
|
|
|
$v_product_cate->seo_title ?: $replace,
|
|
|
|
$v_product_cate->seo_des ?: $replace,
|
|
|
|
$v_product_cate->seo_keywords ?: $replace
|
|
|
|
];
|
|
|
|
$re_trans_product_cate = Translate::translate($need_trans_product_cate, $tls);
|
|
|
|
if (isset($re_trans_product_cate[0]['code']) && $re_trans_product_cate[0]['code'] == 200) {
|
|
|
|
$product_cate_trans_texts = $re_trans_product_cate[0]['texts'];
|
|
|
|
$v_product_cate->title = $product_cate_trans_texts[0] != $replace ? $product_cate_trans_texts[0] : '';
|
|
|
|
$v_product_cate->keywords = $product_cate_trans_texts[1] != $replace ? $product_cate_trans_texts[1] : '';
|
|
|
|
$v_product_cate->describe = $product_cate_trans_texts[2] != $replace ? $product_cate_trans_texts[2] : '';
|
|
|
|
$v_product_cate->seo_title = $product_cate_trans_texts[3] != $replace ? $product_cate_trans_texts[3] : '';
|
|
|
|
$v_product_cate->seo_des = $product_cate_trans_texts[4] != $replace ? $product_cate_trans_texts[4] : '';
|
|
|
|
$v_product_cate->seo_keywords = $product_cate_trans_texts[5] != $replace ? $product_cate_trans_texts[5] : '';
|
|
|
|
$v_product_cate->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 产品分类翻译完成');
|
|
|
|
|
|
|
|
//翻译产品
|
|
|
|
$products = Product::select(['id', 'title', 'intro', 'seo_mate'])->get();
|
|
|
|
foreach ($products as $v_product) {
|
|
|
|
$seo_title = $v_product->seo_mate['title'] ?? '';
|
|
|
|
$seo_keyword = $v_product->seo_mate['keyword'] ?? '';
|
|
|
|
$seo_description = $v_product->seo_mate['description'] ?? '';
|
|
|
|
$need_trans_product = [
|
|
|
|
$v_product->title,
|
|
|
|
$v_product->intro ?: $replace,
|
|
|
|
$seo_title ?: $replace,
|
|
|
|
$seo_keyword ?: $replace,
|
|
|
|
$seo_description ?: $replace
|
|
|
|
];
|
|
|
|
$re_trans_product = Translate::translate($need_trans_product, $tls);
|
|
|
|
if (isset($re_trans_product[0]['code']) && $re_trans_product[0]['code'] == 200) {
|
|
|
|
$product_trans_texts = $re_trans_product[0]['texts'];
|
|
|
|
$trans_seo_mate = [
|
|
|
|
'title' => $product_trans_texts[2] != $replace ? $product_trans_texts[2] : '',
|
|
|
|
'keyword' => $product_trans_texts[3] != $replace ? $product_trans_texts[3] : '',
|
|
|
|
'description' => $product_trans_texts[4] != $replace ? $product_trans_texts[4] : '',
|
|
|
|
];
|
|
|
|
$v_product->title = $product_trans_texts[0] != $replace ? $product_trans_texts[0] : '';
|
|
|
|
$v_product->intro = $product_trans_texts[1] != $replace ? $product_trans_texts[1] : '';
|
|
|
|
$v_product->seo_mate = json_encode($trans_seo_mate);
|
|
|
|
$v_product->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 产品翻译完成');
|
|
|
|
|
|
|
|
//翻译产品详情
|
|
|
|
$products_detail = Detail::select(['id', 'content'])->get();
|
|
|
|
foreach ($products_detail as $v_product_detail) {
|
|
|
|
$content = $v_product_detail->content['content'] ?? '';
|
|
|
|
$need_trans_product_detail = [
|
|
|
|
$content ?: $replace
|
|
|
|
];
|
|
|
|
$re_trans_product_detail = Translate::translate($need_trans_product_detail, $tls);
|
|
|
|
if (isset($re_trans_product_detail[0]['code']) && $re_trans_product_detail[0]['code'] == 200) {
|
|
|
|
$product_detail_trans_texts = $re_trans_product_detail[0]['texts'];
|
|
|
|
$trans_content = [
|
|
|
|
'content' => $product_detail_trans_texts != $replace ? $product_detail_trans_texts : ''
|
|
|
|
];
|
|
|
|
$v_product_detail->content = json_encode($trans_content);
|
|
|
|
$v_product_detail->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 产品详情翻译完成');
|
|
|
|
|
|
|
|
//翻译关键词
|
|
|
|
$keywords = Keyword::select(['id', 'title', 'keyword_title', 'keyword_content', 'seo_title', 'seo_keywords', 'seo_description'])->get();
|
|
|
|
foreach ($keywords as $v_keyword) {
|
|
|
|
$need_trans_keyword = [
|
|
|
|
$v_keyword->title,
|
|
|
|
$v_keyword->keyword_title ?: $replace,
|
|
|
|
$v_keyword->keyword_content ?: $replace,
|
|
|
|
$v_keyword->seo_title ?: $replace,
|
|
|
|
$v_keyword->seo_keywords ?: $replace,
|
|
|
|
$v_keyword->seo_description ?: $replace,
|
|
|
|
];
|
|
|
|
$re_trans_keyword = Translate::translate($need_trans_keyword, $tls);
|
|
|
|
if (isset($re_trans_keyword[0]['code']) && $re_trans_keyword[0]['code'] == 200) {
|
|
|
|
$keyword_trans_texts = $re_trans_keyword[0]['texts'];
|
|
|
|
$v_keyword->title = $keyword_trans_texts[0] != $replace ? $keyword_trans_texts[0] : '';
|
|
|
|
$v_keyword->keyword_title = $keyword_trans_texts[1] != $replace ? $keyword_trans_texts[1] : '';
|
|
|
|
$v_keyword->keyword_content = $keyword_trans_texts[2] != $replace ? $keyword_trans_texts[2] : '';
|
|
|
|
$v_keyword->seo_title = $keyword_trans_texts[3] != $replace ? $keyword_trans_texts[3] : '';
|
|
|
|
$v_keyword->seo_keywords = $keyword_trans_texts[4] != $replace ? $keyword_trans_texts[4] : '';
|
|
|
|
$v_keyword->seo_description = $keyword_trans_texts[5] != $replace ? $keyword_trans_texts[5] : '';
|
|
|
|
$v_keyword->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output('项目ID : ' . $project_id . ' , 产品关键词翻译完成');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 输出处理日志
|
|
|
|
* @param $message
|
|
|
|
*/
|
|
|
|
public function output($message)
|
|
|
|
{
|
|
|
|
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|