Passed
Push — master ( 633df7...87ee59 )
by Fran
03:08
created

CustomTranslateExtension::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base\extension;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\Logger;
6
use PSFS\base\Security;
7
use PSFS\base\types\helpers\I18nHelper;
8
use PSFS\base\types\traits\SingletonTrait;
9
10
/**
11
 * Class CustomTranslateExtension
12
 * @package PSFS\base\extension
13
 */
14
class CustomTranslateExtension extends \Twig_Extension {
15
    use SingletonTrait;
16
17
    const CUSTOM_LOCALE_SESSION_KEY = '__PSFS_CUSTOM_LOCALE_KEY__';
18
19
    protected static $translations = [];
20
    protected static $locale = 'es_ES';
21
    protected static $generate = false;
22
    protected static $filename = '';
23
24
    /**
25
     * @param string $custom_key
26
     * @param bool $force_reload
27
     * @param bool $use_base
28
     */
29 8
    protected static function checkLoad($custom_key = null, $force_reload = false, $use_base = false) {
30 8
        if($force_reload) {
31
            self::dropInstance();
32
        }
33 8
        self::$locale = I18nHelper::extractLocale(Security::getInstance()->getSessionKey(I18nHelper::PSFS_SESSION_LANGUAGE_KEY));
34 8
        self::$generate = (boolean)Config::getParam('i18n.autogenerate', false);
35 8
        if(!$use_base) {
36 8
            $custom_key = $custom_key ?: Security::getInstance()->getSessionKey(self::CUSTOM_LOCALE_SESSION_KEY);
37
        }
38 8
        if(null !== $custom_key) {
39
            Logger::log('[' . self::class . '] Custom key detected: ' . $custom_key, LOG_INFO);
40
            self::$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', $custom_key, self::$locale . '.json']);
0 ignored issues
show
Bug introduced by
Are you sure self::locale of type array|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            self::$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', $custom_key, /** @scrutinizer ignore-type */ self::$locale . '.json']);
Loading history...
41
        } else {
42 8
            self::$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', self::$locale . '.json']);
43
        }
44 8
        if(file_exists(self::$filename)) {
45
            Logger::log('[' . self::class . '] Custom locale detected: ' . $custom_key . ' [' . self::$locale . ']', LOG_INFO);
46
            self::$translations = json_decode(file_get_contents(self::$filename), true);
47 8
        } elseif(null !== $custom_key) {
48
            self::checkLoad(null, $force_reload, true);
49
        }
50 8
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function getTokenParsers()
56
    {
57 1
        return array(new \Twig_Extensions_TokenParser_Trans());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getFilters()
64
    {
65
        return array(
66
            new \Twig_SimpleFilter('trans', function($message) {
67 1
                return self::_($message);
68 1
            }),
69
        );
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getName()
76
    {
77
        return 'PSFSi18n';
78
    }
79
80
    /**
81
     * @param $message
82
     * @param string $custom_key
83
     * @param bool $force_reload
84
     * @return mixed|string
85
     */
86 8
    public static function _($message, $custom_key = null, $force_reload = false) {
87 8
        self::checkLoad($custom_key, $force_reload);
88 8
        if(array_key_exists($message, self::$translations)) {
89
            $translation =  self::$translations[$message];
90
        } else {
91 8
            $translation = gettext($message);
92
        }
93 8
        if(self::$generate) {
94
            self::generate($message, $translation);
95
        }
96 8
        return $translation;
97
    }
98
99
    /**
100
     * @param string $message
101
     * @param string $translation
102
     */
103
    protected static function generate($message, $translation) {
104
        if(!array_key_exists($message, self::$translations)) {
105
            self::$translations[$message] = $translation;
106
        }
107
        file_put_contents(self::$filename, json_encode(array_unique(self::$translations), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
108
    }
109
}