|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Donatas Navidonskis <[email protected]> |
|
5
|
|
|
* @since 2017 |
|
6
|
|
|
* @class LangTranslateAdapter |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class LangTranslateAdapter extends i18nSSLegacyAdapter { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* @param array|string $messageId |
|
14
|
|
|
* @param string $locale |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
public function translate($messageId, $locale = null) { |
|
19
|
|
|
if (class_exists('LangEntity') && class_exists('LangModule')) { |
|
20
|
|
|
$query = DB::query("SHOW TABLES LIKE 'LangEntity'"); |
|
21
|
|
|
|
|
22
|
|
|
if (count($query->column()) > 0) { |
|
23
|
|
|
if ($result = $this->getFromCache($messageId, $locale)) { |
|
|
|
|
|
|
24
|
|
|
return $result; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (! is_array($messageId)) { |
|
28
|
|
|
try { |
|
29
|
|
|
$entity = LangEntity::getByNamespace($messageId); |
|
30
|
|
|
} catch (SS_DatabaseException $ex) { |
|
31
|
|
|
return parent::translate($messageId, $locale); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ($entity) { |
|
35
|
|
|
$this->storeToCache($messageId, $entity->Value, $locale); |
|
36
|
|
|
|
|
37
|
|
|
return $entity->Value; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return parent::translate($messageId, $locale); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function toString() { |
|
50
|
|
|
return static::class; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $namespace |
|
55
|
|
|
* @param string $locale |
|
56
|
|
|
* |
|
57
|
|
|
* @return string|false |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getFromCache($namespace, $locale = null) { |
|
60
|
|
|
$namespace = static::filterCacheKey($namespace); |
|
61
|
|
|
|
|
62
|
|
|
if (! is_null($locale)) { |
|
63
|
|
|
$locale = strtoupper(explode('_', $locale)[0]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$cacheKey = sprintf('_%s_%s', $namespace, $locale); |
|
67
|
|
|
$cache = SS_Cache::factory(LangEditor::class); |
|
68
|
|
|
$result = $cache->load($cacheKey); |
|
69
|
|
|
|
|
70
|
|
|
if ($result) { |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $namespace |
|
79
|
|
|
* @param string $value |
|
80
|
|
|
* @param string $locale |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function storeToCache($namespace, $value, $locale = null) { |
|
85
|
|
|
$namespace = static::filterCacheKey($namespace); |
|
86
|
|
|
|
|
87
|
|
|
if (! is_null($locale)) { |
|
88
|
|
|
$locale = strtoupper(explode('_', $locale)[0]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$cacheKey = sprintf('_%s_%s', $namespace, $locale); |
|
92
|
|
|
$cache = SS_Cache::factory(LangEditor::class); |
|
93
|
|
|
$cache->save($value, $cacheKey); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $value |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function filterCacheKey($value) { |
|
102
|
|
|
return strtolower(trim(preg_replace('~[^a-zA-Z0-9_]+~u', '_', LangFulltextBooleanFilter::convertForeignToLatin($value)))); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.