Completed
Push — master ( ca3395...9b09f7 )
by Tim
02:27
created

TranslateUtility::getLllOrHelpMessage()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 8
cp 0
rs 8.6666
c 0
b 0
f 0
cc 7
nc 6
nop 3
crap 56
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\Core\Utility\VersionNumberUtility;
12
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
13
use TYPO3\CMS\Lang\LanguageService;
14
15
/**
16
 * TranslateUtility.
17
 */
18
class TranslateUtility
19
{
20
    /**
21
     * Assure the translation for the given key.
22
     * If not exists create the label in the xml/xlf file.
23
     * Returns the localization.
24
     *
25
     * Use the Slot to handle the label
26
     *
27
     * @see LocalizationUtility::translate
28
     *
29
     * @param string $key           key in the localization file
30
     * @param string $extensionName
31
     * @param string $default       default value of the label
32
     * @param array  $arguments     arguments are being passed over to vsprintf
33
     * @param string $tableName
34
     *
35
     * @return string
36
     */
37
    public static function assureLabel($key, $extensionName, $default = null, $arguments = null, $tableName = null)
38
    {
39
        if (\is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'])) {
40
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'] as $classConfig) {
41
                $className = GeneralUtility::getUserObj($classConfig);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Utility\G...alUtility::getUserObj() has been deprecated.

This method has been deprecated.

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