TranslateUtility::getLll()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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