-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranslate.php
More file actions
109 lines (90 loc) · 2.77 KB
/
Copy pathTranslate.php
File metadata and controls
109 lines (90 loc) · 2.77 KB
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
<?php
namespace Dreamcoil;
class Translate
{
const COOKIE_NAME = 'lang';
public static function setUpCache()
{
global $dreamcoil_translate_cache;
if(!isset($dreamcoil_translate_cache)) $dreamcoil_translate_cache = [];
}
/**
* Sets the language
*
* @param $lang
*/
public static function setLang($lang)
{
global $console;
setcookie(Translate::COOKIE_NAME, $lang, time() * 2);
return true;
}
public static function getLang()
{
if(isset($_COOKIE['lang'])) return $_COOKIE['lang'];
else return null;
}
public static function destroyLang()
{
setcookie(Translate::COOKIE_NAME, '0', time()-ONE_WEEK);
}
/**
* Gets the translation for a translation key
*
* @param string $key
* @param array $placeholders
* @return string
*/
public static function get($key, $placeholders = [])
{
$config = new \Dreamcoil\Config;
$key = explode('.',$key);
$lang = Translate::getLang();
if (null === $lang) $lang = $config->get('fallback_lang');
$file = Translate::lookUpFile($lang, $key[0]);
$restKey = $key;
unset($restKey[0]);
$restKey = implode(".", $restKey);
if (isset($file[$restKey])) {
$result = $file[$restKey];
if(!defined("DISABLE_DREAMCOIL_TRANSLATE_HTML_ESCAPE")) {
$result = str_replace(
['ü', 'ö', 'ä', 'Ü', 'Ö', 'Ä', 'ß'],
['ü','ö','ä','Ü','Ö','Ä','ß'],
$result
);
}
foreach($placeholders as $name => $value)
{
$name = strtolower($name);
$result = str_replace("%".$name."%", $value, $result);
}
return $result;
}
return implode('.', $key);
}
public static function lookUpFile($lang, $file)
{
global $dreamcoil_translate_cache;
Translate::setUpCache();
if (!isset($dreamcoil_translate_cache[$lang])) $dreamcoil_translate_cache[$lang] = [];
if (!isset($dreamcoil_translate_cache[$lang][$file])) {
$file_path = __DIR__ . '/../files/Translations/' . $lang . '/'. $file . '.php';
if (!file_exists($file_path)) $dreamcoil_translate_cache[$lang][$file] = [];
else $dreamcoil_translate_cache[$lang][$file] = include $file_path;
}
return $dreamcoil_translate_cache[$lang][$file];
}
/**
* Echos a translation
*
* @param $key
* @param null $lang
* @return null
*/
public static function say($key, $placeholders = [])
{
echo Translate::get($key, $placeholders);
return null;
}
}