Files
zzp-server/app/adminapi/logic/setting/LanguagePagLogic.php
2026-01-19 14:19:22 +08:00

236 lines
7.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\logic\setting;
use app\common\service\{UtilsService};
use app\common\model\setting\{LanguagePag,Language};
use app\common\logic\BaseLogic;
use think\facade\Db;
/**
* 语言包逻辑
* Class LanguagePagLogic
* @package app\adminapi\logic\setting
*/
class LanguagePagLogic extends BaseLogic
{
/**
* @notes 添加语言包
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
//查询是否存在
$pag = LanguagePag::where(['lang' => $params['lang'],'type' => $params['type'],'name' => $params['name']])->findOrEmpty();
if (!$pag->isEmpty()) {
throw new \Exception('语言包已存在');
}
LanguagePag::create([
'lang' => $params['lang'],
'type' => $params['type'],
'name' => $params['name'],
'value' => $params['value']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑语言包
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
LanguagePag::where('id', $params['id'])->update([
'lang' => $params['lang'],
'type' => $params['type'],
'name' => $params['name'],
'value' => $params['value']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除语言包
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function delete(array $params): bool
{
return LanguagePag::destroy($params['id']);
}
/**
* @notes 获取语言包详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function detail($params): array
{
return LanguagePag::findOrEmpty($params['id'])->toArray();
}
/**
* @notes 同步语言包
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function sync(array $params): bool
{
Db::startTrans();
try {
//获取源语言
$from = Language::find($params['from_id']);
if ($from->isEmpty()) {
throw new \Exception('源语言不存在');
}
//获取目标语言
$to = Language::find($params['to_id']);
if ($to->isEmpty()) {
throw new \Exception('目标语言不存在');
}
//获取源语言
$pags = LanguagePag::where(['lang' => $from['symbol']])
->order(['type' => 'desc','name' => 'desc'])
->select()
->toArray();
foreach ($pags as &$pag) {
//查询是否存在
$to_pag = LanguagePag::where(['lang' => $to['symbol'],'type' => $pag['type'],'name' => $pag['name']])->findOrEmpty();
if ($to_pag->isEmpty()) {
LanguagePag::create([
'lang' => $to['symbol'],
'type' => $pag['type'],
'name' => $pag['name'],
]);
}
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 翻译语言包
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/14 13:50
*/
public static function trans(array $params): bool
{
Db::startTrans();
try {
//获取源语言
$from = Language::find($params['from_id']);
if ($from->isEmpty()) {
throw new \Exception('源语言不存在');
}
//获取目标语言
$to = Language::find($params['to_id']);
if ($to->isEmpty()) {
throw new \Exception('目标语言不存在');
}
//获取待翻译目标语言
$pags = LanguagePag::where(['lang' => $to['symbol']])
->where(" value IS NULL OR value='' ")
->order(['type' => 'desc','name' => 'desc'])
->select()
->toArray();
$qArray = array();
$idArray = array();
foreach ($pags as &$pag) {
//查询源语言
$from_pag = LanguagePag::where(['lang' => $from['symbol'],'type' => $pag['type'],'name' => $pag['name']])->where(" value IS NOT NULL OR value='' ")->findOrEmpty();
if($from_pag->isEmpty()){
continue;
}else{
array_push($qArray, $from_pag['value']);
array_push($idArray, $pag['id']);
}
}
if(count($qArray) == 0) throw new \Exception('不存在待翻译的内容');
$ret = UtilsService::do_translate($qArray,$from['trans_symbol'],$to['trans_symbol']);
$ret = json_decode($ret, true);
if($ret['errorCode'] != 0){
throw new \Exception('错误代码:'.$ret['errorCode'].',请核对错误代码列表');
}
foreach ($idArray as $key=>$id) {
LanguagePag::update([
'id' => $id,
'value' => $ret['translateResults'][$key]['translation'],
]);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}