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

Yoast / Yoast-SEO-for-TYPO3 / 13327579701

14 Feb 2025 10:43AM UTC coverage: 1.276%. First build
13327579701

push

github

web-flow
Merge pull request #597 from Yoast/feature/v11

[FEATURE] Release 11.0.0

21 of 894 new or added lines in 76 files covered. (2.35%)

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
NEW
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
            return [];
×
36
        }
37

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

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

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

50
        return [];
×
51
    }
52

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

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

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

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

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

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

104
        return $locale;
×
105
    }
106

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

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

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

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

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