A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.6 KiB

6 years ago
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework;
6 years ago
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phacil\Framework\Registry;
/**
* @package Phacil\Framework
*/
class Translate {
6 years ago
/**
*
* @var string|null
*/
6 years ago
private $autoLang;
/**
*
* @var \Phacil\Framework\Session
*/
private $session;
/**
*
* @var \Phacil\Framework\Database
*/
private $db;
/**
*
* @var \Phacil\Framework\Caches
*/
protected $cache;
/**
*
* @var string|null
*/
private $cookie;
/**
*
* @var string
*/
protected $table = 'translate';
6 years ago
public function __construct(){
$this->session = Registry::getInstance()->session;
6 years ago
$this->autoLang = (isset($this->session->data['lang'])) ? $this->session->data['lang'] : NULL;
$this->cookie = (Request::COOKIE('lang')) ?: NULL;
6 years ago
$this->cache = Registry::getInstance()->cache;
$this->db = Registry::getInstance()->db;
6 years ago
if($this->autoLang != NULL) {
setcookie("lang", ($this->autoLang), strtotime( '+90 days' ));
}
}
/**
*
* @param string $table
* @return $this
*/
public function setTranslateTable($table){
$this->table = $table;
return $this;
}
6 years ago
/**
* @param string $value
* @param string|null $lang
* @return string
* @throws PhpfastcacheInvalidArgumentException
*/
6 years ago
public function translation ($value, $lang = NULL) {
6 years ago
$lang = ($lang != NULL) ? $lang : $this->autoLang;
if($this->cache->check("lang_".$lang."_".md5($value))) {
return $this->cache->get("lang_".$lang."_".md5($value));
} else {
$result = $this->db->query()->select()->from($this->table);
$result->where()->equals('text', $value)->end();
$result = $result->load();
6 years ago
if ($result->getNumRows() == 1) {
if($lang && $result->getRow()->getValue($lang) and !empty($result->getRow()->getValue($lang))) {
$this->cache->set("lang_".$lang."_".md5($value), $result->getRow()->getValue($lang), false);
return $result->getRow()->getValue($lang); //valid translation present
6 years ago
} else {
return $value;
}
} elseif($result->getNumRows() < 1) { //message not found in the table
6 years ago
//add unfound message to the table with empties translations
$this->insertBaseText($value);
6 years ago
}
}
return $value;
6 years ago
}
/**
* @param string $value
* @return void
*/
public function insertBaseText ($value){
$this->db->query()->insert()->setTable($this->table)->setValues([
'text' => $value
])->load();
return $this;
6 years ago
}
}