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.

109 lines
2.2 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;
/** @package Phacil\Framework */
6 years ago
final class Translate {
/**
*
* @var string|null
*/
6 years ago
private $autoLang;
/**
*
* @var Request
*/
6 years ago
private $request;
/**
*
* @var Session
*/
6 years ago
private $session;
/**
*
* @var Caches
*/
protected $cache;
6 years ago
public function __construct(){
$this->request = new Request();
$this->session = new Session();
$this->autoLang = (isset($this->session->data['lang'])) ? $this->session->data['lang'] : NULL;
$this->cookie = (isset($this->request->cookie['lang'])) ? $this->request->cookie['lang'] : NULL;
$this->cache = new Caches();
if($this->autoLang != NULL) {
setcookie("lang", ($this->autoLang), strtotime( '+90 days' ));
}
}
/**
* @param string $value
* @param string|null $lang
* @return string
* @throws PhpfastcacheInvalidArgumentException
*/
6 years ago
public function translation ($value, $lang = NULL) {
global $db;
//$cache = new Caches();
$lang = ($lang != NULL) ? $lang : $this->autoLang;
if($this->cache->check("lang_".$lang."_".md5($value))) {
return $this->cache->get("lang_".$lang."_".md5($value));
} else {
$sql = "SELECT * FROM translate WHERE text = '".$db->escape($value)."'";
$result = $db->query($sql, false);
if ($result->num_rows == 1) {
if(isset($result->row[$lang]) and $result->row[$lang] != "") {
$this->cache->set("lang_".$lang."_".md5($value), $result->row[$lang], false);
return $result->row[$lang]; //valid translation present
} else {
return $value;
}
} else { //message not found in the table
//add unfound message to the table with empties translations
$this->insertBaseText($value);
return $value;
}
}
}
/**
* @param string $value
* @return void
*/
6 years ago
public function insertBaseText ($value){
global $db;
$db->query("INSERT INTO translate SET text='".$db->escape($value)."'");
6 years ago
}
}