Completed
Push — master ( a79f71...19c9d7 )
by Tim
12:15
created

Classes/Utility/TranslateUtility.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * TranslateUtility.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
12
13
/**
14
 * TranslateUtility.
15
 */
16
class TranslateUtility
17
{
18
    /**
19
     * Assure the translation for the given key.
20
     * If not exists create the label in the xml/xlf file.
21
     * Returns the localization.
22
     *
23
     * Use the Slot to handle the label
24
     *
25
     * @see LocalizationUtility::translate
26
     *
27
     * @param string $key           key in the localization file
28
     * @param string $extensionName
29
     * @param string $default       default value of the label
30
     * @param array  $arguments     arguments are being passed over to vsprintf
31
     * @param string $tableName
32
     *
33
     * @return string
34
     */
35
    public static function assureLabel($key, $extensionName, $default = null, $arguments = null, $tableName = null)
36
    {
37
        if (\is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'])) {
38
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'] as $classConfig) {
39
                if (class_exists($classConfig)) {
40
                    $object = GeneralUtility::makeInstance($classConfig);
41
                } else {
42
                    // deprecated (!!!)
43
                    $object = GeneralUtility::getUserObj($classConfig);
0 ignored issues
show
The method getUserObj() does not seem to exist on object<TYPO3\CMS\Core\Utility\GeneralUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
                }
45
                if (\is_object($object) && method_exists($object, 'assureLabel')) {
46
                    $object->assureLabel($key, $extensionName, $default, $arguments, $tableName);
47
                }
48
            }
49
        }
50
51
        return (string)$default;
52
    }
53
54
    /**
55
     * Get the given LLL String or render a help message for the user.
56
     *
57
     * @param string $key
58
     * @param string $extensionKey
59
     * @param string $tableName
60
     *
61
     * @return string
62
     */
63
    public static function getLllOrHelpMessage($key, $extensionKey, $tableName = null)
64
    {
65
        $lllString = self::getLllString($key, $extensionKey, null, $tableName);
66
        if (TYPO3_MODE === 'BE' && !isset($GLOBALS['LANG'])) {
67
            $GLOBALS['LANG'] = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageService::class);
68
            $GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
69
        }
70
        if (TYPO3_MODE === 'BE' && null === self::getLll($key, $extensionKey, $tableName)) {
71
            $lllString = self::getLll('pleaseSet', 'autoloader') . $lllString;
72
            if (isset($GLOBALS['LANG'])) {
73
                self::assureLabel($key, $extensionKey, $key, null, $tableName);
74
            }
75
        }
76
77
        return $lllString;
78
    }
79
80
    /**
81
     * Get the correct LLL string for the given key and extension.
82
     *
83
     * @param string $key
84
     * @param        $extensionKey
85
     * @param string $file
86
     * @param string $tableName
87
     *
88
     * @return string
89
     */
90
    public static function getLllString($key, $extensionKey, $file = null, $tableName = null)
91
    {
92
        if (null === $file) {
93
            $file = 'locallang.xlf';
94
        }
95
        if (self::useTableNameFileBase() && null !== $tableName) {
96
            $file = $tableName . '.xlf';
97
        }
98
99
        return 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/' . $file . ':' . $key;
100
    }
101
102
    /**
103
     * Get the translation for the given key.
104
     *
105
     * @param string $key
106
     * @param string $extensionKey
107
     * @param string $tableName
108
     *
109
     * @return string
110
     */
111
    public static function getLll($key, $extensionKey, $tableName = null)
112
    {
113
        $file = self::getLllString($key, $extensionKey, null, $tableName);
114
115
        if (\defined('TYPO3_REQUESTTYPE') && TYPO3_REQUESTTYPE === 16) {
116
            // Do not call the translation workflow in install tool
117
            return $file;
118
        }
119
        if (GeneralUtility::getApplicationContext()->isTesting()) {
120
            // Do not call translation workflow in testinng
121
            return $file;
122
        }
123
124
        return LocalizationUtility::translate($file, $extensionKey);
125
    }
126
127
    /**
128
     * Check if table name file base is used.
129
     *
130
     * @return bool
131
     */
132
    protected static function useTableNameFileBase()
133
    {
134
        $configuration = unserialize((string)$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']);
135
136
        return isset($configuration['enableLanguageFileOnTableBase']) ? (bool)$configuration['enableLanguageFileOnTableBase'] : false;
137
    }
138
}
139