• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 10213662489

02 Aug 2024 09:55AM UTC coverage: 72.717% (-0.05%) from 72.762%
10213662489

push

github

NamelessCoder
[TER] 7.0.4

5533 of 7609 relevant lines covered (72.72%)

13.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.36
/Classes/ViewHelpers/Resource/LanguageViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
12
use FluidTYPO3\Vhs\Utility\ContextUtility;
13
use FluidTYPO3\Vhs\Utility\RequestResolver;
14
use TYPO3\CMS\Core\Localization\LocalizationFactory;
15
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Lang\LanguageService;
18
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19

20
/**
21
 * Resource: Language
22
 *
23
 * Reads a certain language file with returning not just one single label,
24
 * but all the translated labels.
25
 *
26
 * ### Examples
27
 *
28
 * ```
29
 * <!-- Tag usage for force getting labels in a specific language (different to current is possible too) -->
30
 * <v:resource.language extensionName="myext" path="Path/To/Locallang.xlf" languageKey="en"/>
31
 * ```
32
 *
33
 * ```
34
 * <!-- Tag usage for getting labels of current language -->
35
 * <v:resource.language extensionName="myext" path="Path/To/Locallang.xlf"/>
36
 * ```
37
 */
38
class LanguageViewHelper extends AbstractViewHelper
39
{
40
    use TemplateVariableViewHelperTrait;
41

42
    const LOCALLANG_DEFAULT = 'locallang.xlf';
43

44
    /**
45
     * @var boolean
46
     */
47
    protected $escapeOutput = false;
48

49
    public function initializeArguments(): void
50
    {
51
        $this->registerAsArgument();
6✔
52
        $this->registerArgument('extensionName', 'string', 'Name of the extension');
6✔
53
        $this->registerArgument(
6✔
54
            'path',
6✔
55
            'string',
6✔
56
            'Absolute or relative path to the locallang file',
6✔
57
            false,
6✔
58
            static::LOCALLANG_DEFAULT
6✔
59
        );
6✔
60
        $this->registerArgument(
6✔
61
            'languageKey',
6✔
62
            'string',
6✔
63
            'Key for getting translation of a different than current initialized language'
6✔
64
        );
6✔
65
    }
66

67
    /**
68
     * The main render method of this ViewHelper.
69
     *
70
     * @return mixed
71
     */
72
    public function render()
73
    {
74
        $path = $this->getResolvedPath();
6✔
75
        $languageKey = $this->getLanguageKey();
6✔
76
        /** @var LocalizationFactory $languageFactory */
77
        $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
6✔
78
        $locallang = (array) $languageFactory->getParsedData($path, $languageKey);
6✔
79
        $labels = $this->getLabelsByLanguageKey($locallang, $languageKey);
6✔
80
        $labels = $this->getLabelsFromTarget($labels);
6✔
81
        return $this->renderChildrenWithVariableOrReturnInput($labels);
6✔
82
    }
83

84
    /**
85
     * Gets the extension name from defined argument or
86
     * tries to resolve it from the controller context if not set.
87
     */
88
    protected function getResolvedExtensionName(): ?string
89
    {
90
        /** @var string|null $extensionName */
91
        $extensionName = $this->arguments['extensionName'];
6✔
92

93
        return $extensionName
6✔
94
            ?? RequestResolver::resolveControllerExtensionNameFromRenderingContext($this->renderingContext);
6✔
95
    }
96

97
    /**
98
     * Gets the resolved file path with trying to resolve relative paths even if no
99
     * extension key is defined.
100
     */
101
    protected function getResolvedPath(): string
102
    {
103
        /** @var string $path */
104
        $path = $this->arguments['path'];
6✔
105
        $absoluteFileName = GeneralUtility::getFileAbsFileName($path);
6✔
106

107
        if (!file_exists($absoluteFileName) && ($extensionName = $this->getResolvedExtensionName())) {
6✔
108
            $extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
6✔
109
            $absoluteFileName = ExtensionManagementUtility::extPath($extensionKey, $path);
6✔
110
        }
111

112
        return $absoluteFileName;
6✔
113
    }
114

115
    /**
116
     * Gets the translated labels by a specific language key
117
     * or fallback to 'default'.
118
     */
119
    protected function getLabelsByLanguageKey(array $locallang, string $languageKey): array
120
    {
121
        $labels = [];
6✔
122

123
        if (!empty($locallang[$languageKey])) {
6✔
124
            $labels = $locallang[$languageKey];
×
125
        } elseif (!empty($locallang['default'])) {
6✔
126
            $labels = $locallang['default'];
×
127
        }
128

129
        return $labels;
6✔
130
    }
131

132
    /**
133
     * Simplify label array with just taking the value from target.
134
     */
135
    protected function getLabelsFromTarget(array $labels): array
136
    {
137
        foreach ($labels as $labelKey => $label) {
6✔
138
            $labels[$labelKey] = $label[0]['target'];
×
139
        }
140

141
        return $labels;
6✔
142
    }
143

144
    /**
145
     * Gets the language key from arguments or from current
146
     * initialized language if argument is not defined.
147
     */
148
    protected function getLanguageKey(): string
149
    {
150
        /** @var string|null $languageKey */
151
        $languageKey = $this->arguments['languageKey'];
6✔
152
        return $languageKey ?? $this->getInitializedLanguage();
6✔
153
    }
154

155
    /**
156
     * Gets the key of current initialized language
157
     * or fallback to 'default'.
158
     */
159
    protected function getInitializedLanguage(): string
160
    {
161
        $language = 'default';
6✔
162

163
        if (ContextUtility::isFrontend()) {
6✔
164
            $language = $GLOBALS['TSFE']->lang;
6✔
165
        } elseif ($GLOBALS['LANG'] instanceof LanguageService) {
×
166
            $language = $GLOBALS['LANG']->lang;
×
167
        }
168

169
        return (string) $language;
6✔
170
    }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc