Spaces:
Runtime error
Runtime error
File size: 4,430 Bytes
8d21059 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | <?php
class Languages
{
# Translations ID on Redis (Optional) [String]
public $project_name = 'SGRB';
# Translations file name (Required) [String]
public $file_name = 'translations.json';
# The cache time (Optional, Required for Redis) [false: no cache/int: time in seconds]
private $cache_time = 60 * 60 * 3;
# Default user language (Required) [String of language ID]
private $user_language = 'en';
# NeleBot X Database class (Optional, Required for Redis) [Database Class]
private $db = [];
# Load configs
function __construct($user_language = 'en', $db = []) {
$this->user_language = $user_language;
if (isset($db->configs) and $db->configs['redis']['status']) {
$this->db = $db;
$this->redisCheck();
} else {
if (!$this->translations = json_decode(file_get_contents($this->file_name), true)) {
return ['ok' => 0, 'error_code' => 500, 'description' => 'Unable to get translations data!'];
}
}
}
# Set the default language
public function setLanguage($user_language = 'en') {
return $this->user_language = $user_language;
}
# Load the translations on Redis for more speed (Redis only)
private function redisCheck () {
if (!$this->db->rget('tr-' . $this->project_name . '-status')) {
$this->db->rset('tr-' . $this->project_name . '-status', true, $this->cache_time);
$trs = $this->getAllTranslations();
if ($trs['ok']) {
$this->db->rdel($this->db->rkeys('tr-' . $this->project_name . '*'));
$this->db->rset('tr-' . $this->project_name . '-status', true, $this->cache_time);
foreach ($trs['result'] as $lang => $strings) {
foreach($strings as $stringn => $translation) {
$this->db->rset('tr-' . $this->project_name . '-' . $lang . '-' . $stringn, $translation, $this->cache_time);
}
}
} else {
$this->db->rdel('tr-' . $this->project_name . '-status');
}
return 1;
}
return;
}
# Reload translations
public function reload () {
if (isset($this->db->configs) and $this->db->configs['redis']['status']) {
$this->db->rdel('tr-' . $this->project_name . '-status');
$this->redisCheck();
}
return 1;
}
# Get the translation from string ID
public function getTranslation($string, $args = [], $user_lang = 'def') {
if ($user_lang == 'def') {
$lang = $this->user_language;
} else {
$lang = strtolower($user_lang);
}
$string = str_replace(' ', '', $string);
if (isset($this->db->configs)) {
if ($lang !== 'en' and $t_string = $this->db->rget('tr-' . $this->project_name . '-' . $lang . '-' . $string)) {
} elseif ($t_string = $this->db->rget('tr-' . $this->project_name . '-en-' . $string)) {
} else {
$t_string = '👾: ' . $string;
}
} else {
if ($lang !== 'en' and $t_string = $this->translations[$lang][$string]) {
} elseif ($t_string = $this->translations['en'][$string]) {
} else {
$t_string = '🤖';
}
}
if (!empty($args) and is_array($args)) {
$args = array_values($args);
foreach(range(0, count($args) - 1) as $num) {
$t_string = str_replace('[' . $num . ']', $args[$num], $t_string);
}
}
return $t_string;
}
# Get all translations from the file, oneskyapp or from the current script
public function getAllTranslations () {
if (isset($this->translations)) {
return ['ok' => 1, 'result' => $this->translations];
} elseif (file_exists($this->file_name)) {
$file = file_get_contents($this->file_name);
if ($file) {
if ($translations = json_decode($file, 1)) {
return ['ok' => 1, 'result' => $translations];
}
return ['ok' => 1, 'result' => [], 'notice' => 'Failed to get JSON format from the file!'];
}
return ['ok' => 0, 'result' => [], 'notice' => 'The file is empty!'];
} else {
return ['ok' => 1, 'result' => [], 'notice' => 'No configs for translations'];
}
}
public function save ($array1, $array2) {
$array = [];
foreach ($array1 as $lang => $strings) {
if (!is_array($array[$lang])) $array[$lang] = [];
foreach ($strings as $strName => $string) {
$array[$lang][$strName] = $string;
}
}
foreach ($array2 as $lang => $strings) {
if (!is_array($array[$lang])) $array[$lang] = [];
foreach ($strings as $strName => $string) {
$array[$lang][$strName] = $string;
}
}
return $array;
}
}
?>
|