Completed
Push — master ( d3c055...25d723 )
by Tim
13:13
created

TranslateUtility::getLllOrHelpMessage()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 6
cp 0
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 3
crap 42
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
Bug introduced by
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()) {
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Utility\G...getApplicationContext() has been deprecated with message: since TYPO3 v10.2, will be removed in TYPO3 v11, use Environment::getContext() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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