Completed
Push — master ( 86d3fe...d815fa )
by Tim
10:20
created

TranslateUtility::useTableNameFileBase()   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
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * TranslateUtility.
4
 */
5
namespace HDNET\Autoloader\Utility;
6
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
9
use TYPO3\CMS\Lang\LanguageService;
10
11
/**
12
 * TranslateUtility.
13
 */
14
class TranslateUtility
15
{
16
    /**
17
     * Assure the translation for the given key.
18
     * If not exists create the label in the xml/xlf file.
19
     * Returns the localization.
20
     *
21
     * Use the Slot to handle the label
22
     *
23
     * @see LocalizationUtility::translate
24
     *
25
     * @param string $key key in the localization file
26
     * @param string $extensionName
27
     * @param string $default default value of the label
28
     * @param array $arguments arguments are being passed over to vsprintf
29
     * @param string $tableName
30
     *
31
     * @return string
32
     */
33
    public static function assureLabel($key, $extensionName, $default = null, $arguments = null, $tableName = null)
34
    {
35
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'])) {
36
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['autoloader']['assureLabel'] as $classConfig) {
37
                $className = GeneralUtility::getUserObj($classConfig);
38
                if (is_object($className) && method_exists($className, 'assureLabel')) {
39
                    $className->assureLabel($key, $extensionName, $default, $arguments, $tableName);
40
                }
41
            }
42
        }
43
44
        return (string) $default;
45
    }
46
47
    /**
48
     * Get the given LLL String or render a help message for the user.
49
     *
50
     * @param string $key
51
     * @param string $extensionKey
52
     * @param string $tableName
53
     *
54
     * @return string
55
     */
56
    public static function getLllOrHelpMessage($key, $extensionKey, $tableName = null)
57
    {
58
        $lllString = self::getLllString($key, $extensionKey, null, $tableName);
59
        if (TYPO3_MODE === 'BE' && !isset($GLOBALS['LANG'])) {
60
            // init for backend
61
            $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
62
            $GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
63
        }
64
        if (TYPO3_MODE === 'BE' && null === self::getLll($key, $extensionKey, $tableName)) {
65
            $lllString = self::getLll('pleaseSet', 'autoloader') . $lllString;
66
            if (isset($GLOBALS['LANG'])) {
67
                self::assureLabel($key, $extensionKey, $key, null, $tableName);
68
            }
69
        }
70
71
        return $lllString;
72
    }
73
74
    /**
75
     * Get the correct LLL string for the given key and extension.
76
     *
77
     * @param string $key
78
     * @param        $extensionKey
79
     * @param string $file
80
     * @param string $tableName
81
     *
82
     * @return string
83
     */
84
    public static function getLllString($key, $extensionKey, $file = null, $tableName = null)
85
    {
86
        if($file === null) {
87
            $file = 'locallang.xlf';
88
        }
89
        if(self::useTableNameFileBase() && $tableName !== null) {
90
            $file = $tableName . '.xlf';
91
        }
92
        return 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/' . $file . ':' . $key;
93
    }
94
95
    /**
96
     * Get the translation for the given key.
97
     *
98
     * @param string $key
99
     * @param string $extensionKey
100
     * @param string $tableName
101
     *
102
     * @return string
103
     */
104
    public static function getLll($key, $extensionKey, $tableName = null)
105
    {
106
        $file = self::getLllString($key, $extensionKey, null, $tableName);
107
        return LocalizationUtility::translate($file, $extensionKey);
108
    }
109
110
111
    /**
112
     * Check if table name file base is used
113
     *
114
     * @return bool
115
     */
116
    static protected function useTableNameFileBase()
117
    {
118
        $configuration = unserialize((string)$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']);
119
        return isset($configuration['enableLanguageFileOnTableBase']) ? (bool)$configuration['enableLanguageFileOnTableBase'] : false;
120
    }
121
}
122