1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base\extension; |
3
|
|
|
|
4
|
|
|
use PSFS\base\Logger; |
5
|
|
|
use PSFS\base\Security; |
6
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
7
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CustomTranslateExtension |
11
|
|
|
* @package PSFS\base\extension |
12
|
|
|
*/ |
13
|
|
|
class CustomTranslateExtension extends \Twig_Extension { |
14
|
|
|
use SingletonTrait; |
15
|
|
|
|
16
|
|
|
const CUSTOM_LOCALE_SESSION_KEY = '__PSFS_CUSTOM_LOCALE_KEY__'; |
17
|
|
|
|
18
|
|
|
protected static $translations = []; |
19
|
|
|
protected static $locale = 'es_ES'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $custom_key |
|
|
|
|
23
|
|
|
* @param bool $force_reload |
24
|
|
|
*/ |
25
|
1 |
|
protected static function checkLoad($custom_key = null, $force_reload = false) { |
26
|
1 |
|
if($force_reload) self::dropInstance(); |
27
|
1 |
|
self::$locale = I18nHelper::extractLocale('es_ES'); |
28
|
1 |
|
$custom_key = $custom_key ?: Security::getInstance()->getSessionKey(self::CUSTOM_LOCALE_SESSION_KEY); |
29
|
1 |
|
if(null !== $custom_key) { |
30
|
|
|
Logger::log('[' . self::class . '] Custom key detected: ' . $custom_key, LOG_INFO); |
31
|
|
|
$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', $custom_key, self::$locale . '.json']); |
32
|
|
|
if(file_exists($filename)) { |
33
|
|
|
Logger::log('[' . self::class . '] Custom locale detected: ' . $custom_key . ' [' . self::$locale . ']', LOG_INFO); |
|
|
|
|
34
|
|
|
self::$translations = json_decode(file_get_contents($filename), true); |
35
|
|
|
} |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function getTokenParsers() |
43
|
|
|
{ |
44
|
1 |
|
return array(new \Twig_Extensions_TokenParser_Trans()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getFilters() |
51
|
|
|
{ |
52
|
|
|
return array( |
53
|
1 |
|
new \Twig_SimpleFilter('trans', function($message) { |
54
|
1 |
|
return self::_($message); |
55
|
1 |
|
}), |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
1 |
|
public function getName() |
63
|
|
|
{ |
64
|
1 |
|
return 'PSFSi18n'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $message |
69
|
|
|
* @param string $custom_key |
|
|
|
|
70
|
|
|
* @param bool $force_reload |
71
|
|
|
* @return mixed|string |
72
|
|
|
*/ |
73
|
1 |
|
public static function _($message, $custom_key = null, $force_reload = false) { |
74
|
1 |
|
self::checkLoad($custom_key, $force_reload); |
75
|
1 |
|
if(array_key_exists($message, self::$translations)) { |
76
|
|
|
return self::$translations[$message]; |
77
|
|
|
} else { |
78
|
1 |
|
return gettext($message); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.