作者 刘锟

合并分支 'akun' 到 'master'

Akun



查看合并请求 !995
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Translate;
  4 +
  5 +use App\Helper\Translate;
  6 +use App\Models\News\News;
  7 +use App\Models\News\NewsCategory;
  8 +use App\Models\Product\Category;
  9 +use App\Models\Product\Detail;
  10 +use App\Models\Product\Keyword;
  11 +use App\Models\Product\Product;
  12 +use App\Services\ProjectServer;
  13 +use Illuminate\Console\Command;
  14 +
  15 +class SiteTranslate extends Command
  16 +{
  17 + /**
  18 + * The name and signature of the console command.
  19 + *
  20 + * @var string
  21 + */
  22 + protected $signature = 'site_translate {project_id} {tls}';
  23 +
  24 + /**
  25 + * The console command description.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $description = '站点产品,新闻,博客整站翻译';
  30 +
  31 + public function handle()
  32 + {
  33 + //项目id
  34 + $project_id = $this->argument('project_id');
  35 + //翻译目标语种
  36 + $tls = $this->argument('tls');
  37 +
  38 + //值为空的替换符
  39 + $replace = '!@#$%';
  40 +
  41 + ProjectServer::useProject($project_id);
  42 +
  43 + $this->output('项目ID : ' . $project_id . ' , 整站内容翻译开始 , 翻译语种 : ' . $tls);
  44 +
  45 + //翻译新闻分类
  46 + $news_category = NewsCategory::select(['id', 'name', 'remark', 'seo_title', 'seo_des', 'seo_keywords'])->get();
  47 + foreach ($news_category as $v_news_cate) {
  48 + $need_trans_news_cate = [
  49 + $v_news_cate->name,
  50 + $v_news_cate->remark ?: $replace,
  51 + $v_news_cate->seo_title ?: $replace,
  52 + $v_news_cate->seo_des ?: $replace,
  53 + $v_news_cate->seo_keywords ?: $replace
  54 + ];
  55 + $re_trans_news_cate = Translate::translate($need_trans_news_cate, $tls);
  56 + if (isset($re_trans_news_cate[0]['code']) && $re_trans_news_cate[0]['code'] == 200) {
  57 + $news_cate_trans_texts = $re_trans_news_cate[0]['texts'];
  58 + $v_news_cate->name = $news_cate_trans_texts[0] != $replace ? $news_cate_trans_texts[0] : '';
  59 + $v_news_cate->remark = $news_cate_trans_texts[1] != $replace ? $news_cate_trans_texts[1] : '';
  60 + $v_news_cate->seo_title = $news_cate_trans_texts[2] != $replace ? $news_cate_trans_texts[2] : '';
  61 + $v_news_cate->seo_des = $news_cate_trans_texts[3] != $replace ? $news_cate_trans_texts[3] : '';
  62 + $v_news_cate->seo_keywords = $news_cate_trans_texts[4] != $replace ? $news_cate_trans_texts[4] : '';
  63 + $v_news_cate->save();
  64 + }
  65 + }
  66 +
  67 + $this->output('项目ID : ' . $project_id . ' , 新闻分类翻译完成');
  68 +
  69 + //翻译新闻
  70 + $news = News::select(['id', 'name', 'text', 'remark', 'seo_title', 'seo_description', 'seo_keywords'])->get();
  71 + foreach ($news as $v_news) {
  72 + $need_trans_news = [
  73 + $v_news->name,
  74 + $v_news->text ?: $replace,
  75 + $v_news->remark ?: $replace,
  76 + $v_news->seo_title ?: $replace,
  77 + $v_news->seo_description ?: $replace,
  78 + $v_news->seo_keywords ?: $replace
  79 + ];
  80 + $re_trans_news = Translate::translate($need_trans_news, $tls);
  81 + if (isset($re_trans_news[0]['code']) && $re_trans_news[0]['code'] == 200) {
  82 + $news_trans_texts = $re_trans_news[0]['texts'];
  83 + $v_news->name = $news_trans_texts[0] != $replace ? $news_trans_texts[0] : '';
  84 + $v_news->text = $news_trans_texts[1] != $replace ? $news_trans_texts[1] : '';
  85 + $v_news->remark = $news_trans_texts[2] != $replace ? $news_trans_texts[2] : '';
  86 + $v_news->seo_title = $news_trans_texts[3] != $replace ? $news_trans_texts[3] : '';
  87 + $v_news->seo_description = $news_trans_texts[4] != $replace ? $news_trans_texts[4] : '';
  88 + $v_news->seo_keywords = $news_trans_texts[5] != $replace ? $news_trans_texts[5] : '';
  89 + $v_news->save();
  90 + }
  91 + }
  92 +
  93 + $this->output('项目ID : ' . $project_id . ' , 新闻翻译完成');
  94 +
  95 + //翻译产品分类
  96 + $product_category = Category::select(['id', 'title', 'keywords', 'describe', 'seo_title', 'seo_des', 'seo_keywords'])->get();
  97 + foreach ($product_category as $v_product_cate) {
  98 + $need_trans_product_cate = [
  99 + $v_product_cate->title,
  100 + $v_product_cate->keywords ?: $replace,
  101 + $v_product_cate->describe ?: $replace,
  102 + $v_product_cate->seo_title ?: $replace,
  103 + $v_product_cate->seo_des ?: $replace,
  104 + $v_product_cate->seo_keywords ?: $replace
  105 + ];
  106 + $re_trans_product_cate = Translate::translate($need_trans_product_cate, $tls);
  107 + if (isset($re_trans_product_cate[0]['code']) && $re_trans_product_cate[0]['code'] == 200) {
  108 + $product_cate_trans_texts = $re_trans_product_cate[0]['texts'];
  109 + $v_product_cate->title = $product_cate_trans_texts[0] != $replace ? $product_cate_trans_texts[0] : '';
  110 + $v_product_cate->keywords = $product_cate_trans_texts[1] != $replace ? $product_cate_trans_texts[1] : '';
  111 + $v_product_cate->describe = $product_cate_trans_texts[2] != $replace ? $product_cate_trans_texts[2] : '';
  112 + $v_product_cate->seo_title = $product_cate_trans_texts[3] != $replace ? $product_cate_trans_texts[3] : '';
  113 + $v_product_cate->seo_des = $product_cate_trans_texts[4] != $replace ? $product_cate_trans_texts[4] : '';
  114 + $v_product_cate->seo_keywords = $product_cate_trans_texts[5] != $replace ? $product_cate_trans_texts[5] : '';
  115 + $v_product_cate->save();
  116 + }
  117 + }
  118 +
  119 + $this->output('项目ID : ' . $project_id . ' , 产品分类翻译完成');
  120 +
  121 + //翻译产品
  122 + $products = Product::select(['id', 'title', 'intro', 'seo_mate'])->get();
  123 + foreach ($products as $v_product) {
  124 + $seo_title = $v_product->seo_mate['title'] ?? '';
  125 + $seo_keyword = $v_product->seo_mate['keyword'] ?? '';
  126 + $seo_description = $v_product->seo_mate['description'] ?? '';
  127 + $need_trans_product = [
  128 + $v_product->title,
  129 + $v_product->intro ?: $replace,
  130 + $seo_title ?: $replace,
  131 + $seo_keyword ?: $replace,
  132 + $seo_description ?: $replace
  133 + ];
  134 + $re_trans_product = Translate::translate($need_trans_product, $tls);
  135 + if (isset($re_trans_product[0]['code']) && $re_trans_product[0]['code'] == 200) {
  136 + $product_trans_texts = $re_trans_product[0]['texts'];
  137 + $trans_seo_mate = [
  138 + 'title' => $product_trans_texts[2] != $replace ? $product_trans_texts[2] : '',
  139 + 'keyword' => $product_trans_texts[3] != $replace ? $product_trans_texts[3] : '',
  140 + 'description' => $product_trans_texts[4] != $replace ? $product_trans_texts[4] : '',
  141 + ];
  142 + $v_product->title = $product_trans_texts[0] != $replace ? $product_trans_texts[0] : '';
  143 + $v_product->intro = $product_trans_texts[1] != $replace ? $product_trans_texts[1] : '';
  144 + $v_product->seo_mate = json_encode($trans_seo_mate);
  145 + $v_product->save();
  146 + }
  147 + }
  148 +
  149 + $this->output('项目ID : ' . $project_id . ' , 产品翻译完成');
  150 +
  151 + //翻译产品详情
  152 + $products_detail = Detail::select(['id', 'content'])->get();
  153 + foreach ($products_detail as $v_product_detail) {
  154 + $content = $v_product_detail->content['content'] ?? '';
  155 + $need_trans_product_detail = [
  156 + $content ?: $replace
  157 + ];
  158 + $re_trans_product_detail = Translate::translate($need_trans_product_detail, $tls);
  159 + if (isset($re_trans_product_detail[0]['code']) && $re_trans_product_detail[0]['code'] == 200) {
  160 + $product_detail_trans_texts = $re_trans_product_detail[0]['texts'];
  161 + $trans_content = [
  162 + 'content' => $product_detail_trans_texts != $replace ? $product_detail_trans_texts : ''
  163 + ];
  164 + $v_product_detail->content = json_encode($trans_content);
  165 + $v_product_detail->save();
  166 + }
  167 + }
  168 +
  169 + $this->output('项目ID : ' . $project_id . ' , 产品详情翻译完成');
  170 +
  171 + //翻译关键词
  172 + $keywords = Keyword::select(['id', 'title', 'keyword_title', 'keyword_content', 'seo_title', 'seo_keywords', 'seo_description'])->get();
  173 + foreach ($keywords as $v_keyword) {
  174 + $need_trans_keyword = [
  175 + $v_keyword->title,
  176 + $v_keyword->keyword_title ?: $replace,
  177 + $v_keyword->keyword_content ?: $replace,
  178 + $v_keyword->seo_title ?: $replace,
  179 + $v_keyword->seo_keywords ?: $replace,
  180 + $v_keyword->seo_description ?: $replace,
  181 + ];
  182 + $re_trans_keyword = Translate::translate($need_trans_keyword, $tls);
  183 + if (isset($re_trans_keyword[0]['code']) && $re_trans_keyword[0]['code'] == 200) {
  184 + $keyword_trans_texts = $re_trans_keyword[0]['texts'];
  185 + $v_keyword->title = $keyword_trans_texts[0] != $replace ? $keyword_trans_texts[0] : '';
  186 + $v_keyword->keyword_title = $keyword_trans_texts[1] != $replace ? $keyword_trans_texts[1] : '';
  187 + $v_keyword->keyword_content = $keyword_trans_texts[2] != $replace ? $keyword_trans_texts[2] : '';
  188 + $v_keyword->seo_title = $keyword_trans_texts[3] != $replace ? $keyword_trans_texts[3] : '';
  189 + $v_keyword->seo_keywords = $keyword_trans_texts[4] != $replace ? $keyword_trans_texts[4] : '';
  190 + $v_keyword->seo_description = $keyword_trans_texts[5] != $replace ? $keyword_trans_texts[5] : '';
  191 + $v_keyword->save();
  192 + }
  193 + }
  194 +
  195 + $this->output('项目ID : ' . $project_id . ' , 产品关键词翻译完成');
  196 + }
  197 +
  198 +
  199 + /**
  200 + * 输出处理日志
  201 + * @param $message
  202 + */
  203 + public function output($message)
  204 + {
  205 + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
  206 + }
  207 +}