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

Yoast / Yoast-SEO-for-TYPO3 / 13931841650

18 Mar 2025 07:27PM UTC coverage: 1.276%. Remained the same
13931841650

push

github

RinyVT
[BUGFIX] Fallback to en_GB if no backend locale is found to prevent missing javascript translations

0 of 1 new or added line in 1 file covered. (0.0%)

35 of 2744 relevant lines covered (1.28%)

0.04 hits per line

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

0.0
/Classes/Service/LocaleService.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace YoastSeoForTypo3\YoastSeo\Service;
6

7
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
8
use TYPO3\CMS\Core\Localization\Locales;
9
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
10
use TYPO3\CMS\Core\Site\SiteFinder;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use YoastSeoForTypo3\YoastSeo\Traits\BackendUserTrait;
13
use YoastSeoForTypo3\YoastSeo\Traits\LanguageServiceTrait;
14

15
class LocaleService
16
{
17
    use BackendUserTrait;
18
    use LanguageServiceTrait;
19

20
    protected const APP_TRANSLATION_FILE_PATTERN = 'EXT:yoast_seo/Resources/Private/Language/wordpress-seo-%s.json';
21

22
    public function __construct(
23
        protected Locales $locales,
24
        protected SiteFinder $siteFinder
25
    ) {}
×
26

27
    /**
28
     * @return array<string, array<string, string>>
29
     */
30
    public function getTranslations(): array
31
    {
32
        $interfaceLocale = $this->getInterfaceLocale();
×
33

34
        if ($interfaceLocale === null) {
×
35
            // Fall back to English if no suitable locale could be resolved to prevent missing translations
NEW
36
            $interfaceLocale = 'en_GB';
×
37
        }
38

39
        $translationFilePath = GeneralUtility::getFileAbsFileName(
×
40
            sprintf(static::APP_TRANSLATION_FILE_PATTERN, $interfaceLocale)
×
41
        );
×
42

43
        if ($translationFilePath === '' || !file_exists($translationFilePath)) {
×
44
            return [];
×
45
        }
46

47
        if ($jsonContents = file_get_contents($translationFilePath)) {
×
48
            return json_decode($jsonContents, true);
×
49
        }
50

51
        return [];
×
52
    }
53

54
    /**
55
     * Try to resolve a supported locale based on the user settings
56
     * take the configured locale dependencies into account
57
     * so if the TYPO3 interface is tailored for a specific dialect
58
     * the local of a parent language might be used
59
     *
60
     * @return string|null
61
     */
62
    protected function getInterfaceLocale(): ?string
63
    {
64
        $locale = null;
×
65

66
        $translationConfiguration = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['translations'] ?? [
×
67
            'availableLocales' => [],
×
68
            'languageKeyToLocaleMapping' => [],
×
69
        ];
×
70

71
        $userLanguage = $this->getBackendUser()->user['lang'] ?? '';
×
72
        if (empty($userLanguage) || $userLanguage === 'default') {
×
73
            $userLanguage = 'en';
×
74
        }
75

76
        $languageChain = $this->locales->getLocaleDependencies($userLanguage);
×
77
        array_unshift($languageChain, $userLanguage);
×
78

79
        // try to find a matching locale available for this plugins UI
80
        // take configured locale dependencies into account
81
        if ($languageChain !== null) {
×
82
            $suitableLocales = array_intersect(
×
83
                $languageChain,
×
84
                $translationConfiguration['availableLocales']
×
85
            );
×
86
            if (count($suitableLocales) > 0) {
×
87
                $locale = array_shift($suitableLocales);
×
88
            }
89
        }
90

91
        // if a locale couldn't be resolved try if an entry of the
92
        // language dependency chain matches legacy mapping
93
        if ($locale === null && $languageChain !== null) {
×
94
            $suitableLanguageKeys = array_intersect(
×
95
                $languageChain,
×
96
                array_flip(
×
97
                    $translationConfiguration['languageKeyToLocaleMapping']
×
98
                )
×
99
            );
×
100
            if (count($suitableLanguageKeys) > 0) {
×
101
                $locale = $translationConfiguration['languageKeyToLocaleMapping'][array_shift($suitableLanguageKeys)];
×
102
            }
103
        }
104

105
        return $locale;
×
106
    }
107

108
    public function getLocale(int $pageId, int &$languageId): ?string
109
    {
110
        try {
111
            $site = $this->siteFinder->getSiteByPageId($pageId);
×
112
            if ($languageId === -1) {
×
113
                $languageId = $site->getDefaultLanguage()->getLanguageId();
×
114
                return $this->getLanguageCode($site->getDefaultLanguage());
×
115
            }
116
            return $this->getLanguageCode($site->getLanguageById($languageId));
×
117
        } catch (SiteNotFoundException|\InvalidArgumentException) {
×
118
            return null;
×
119
        }
120
    }
121

122
    protected function getLanguageCode(SiteLanguage $siteLanguage): string
123
    {
124
        // Support for v11
125
        if (method_exists($siteLanguage, 'getTwoLetterIsoCode')) {
×
126
            return $siteLanguage->getTwoLetterIsoCode();
×
127
        }
128
        return $siteLanguage->getLocale()->getLanguageCode();
×
129
    }
130

131
    /**
132
     * @param array<string, mixed> $data
133
     */
134
    public function getLanguageIdFromData(array $data): int
135
    {
136
        if (!isset($data['databaseRow']['sys_language_uid'])) {
×
137
            return 0;
×
138
        }
139

140
        if (is_array($data['databaseRow']['sys_language_uid']) && count($data['databaseRow']['sys_language_uid']) > 0) {
×
141
            return (int)current($data['databaseRow']['sys_language_uid']);
×
142
        }
143
        return (int)$data['databaseRow']['sys_language_uid'];
×
144
    }
145

146
    /**
147
     * @return array<int, string>
148
     */
149
    public function getSupportedLanguages(): array
150
    {
151
        return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['supportedLanguages'] ?? [];
×
152
    }
153
}
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