LocaleApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Api;
15
16
use Symfony\Component\DependencyInjection\Attribute\Autowire;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\Intl\Locales;
20
use Zikula\CoreBundle\Api\ApiInterface\LocaleApiInterface;
21
22
class LocaleApi implements LocaleApiInterface
23
{
24
    /**
25
     * Locales with translations present
26
     */
27
    private array $supportedLocales = [];
28
29
    private string $translationPath;
30
31
    private string $sectionKey;
32
33
    public function __construct(
34
        private readonly RequestStack $requestStack,
35
        #[Autowire(param: 'kernel.project_dir')]
36
        private readonly string $projectDir,
37
        private readonly bool $multiLingualEnabled,
38
        #[Autowire(param: 'kernel.default_locale')]
39
        private readonly string $defaultLocale
40
    ) {
41
        $this->supportedLocales = [
42
            'withRegions' => [],
43
            'withoutRegions' => []
44
        ];
45
        $this->translationPath = $this->projectDir . '/translations';
46
    }
47
48
    public function multilingual(): bool
49
    {
50
        return $this->multiLingualEnabled;
51
    }
52
53
    public function getSupportedLocales(bool $includeRegions = true): array
54
    {
55
        $this->sectionKey = $includeRegions ? 'withRegions' : 'withoutRegions';
56
57
        if (!empty($this->supportedLocales[$this->sectionKey])) {
58
            return $this->supportedLocales[$this->sectionKey];
59
        }
60
61
        $this->supportedLocales[$this->sectionKey][] = $this->defaultLocale;
62
63
        if (!is_dir($this->translationPath)) {
64
            return $this->supportedLocales[$this->sectionKey];
65
        }
66
67
        if ($this->multiLingualEnabled) {
68
            // read in locales from translation path
69
            $this->collectLocales($includeRegions);
70
        }
71
72
        return $this->supportedLocales[$this->sectionKey];
73
    }
74
75
    public function getSupportedLocaleNames(string $region = null, string $displayLocale = null, bool $includeRegions = true): array
76
    {
77
        $locales = $this->getSupportedLocales($includeRegions);
78
        $namedLocales = [];
79
        foreach ($locales as $locale) {
80
            $localeName = Locales::getName($locale, $displayLocale);
81
            $namedLocales[ucfirst($localeName)] = $locale;
82
        }
83
        ksort($namedLocales);
84
85
        return $namedLocales;
86
    }
87
88
    private function getTranslationFiles(): Finder
89
    {
90
        $finder = new Finder();
91
        $files = $finder->files()
92
            ->in([$this->translationPath])
93
            ->depth(0)
94
            ->name(['*.csv', '*.dat', '*.ini', '*.mo', '*.php', '*.po', '*.qt', '*.xlf', '*.json', '*.yaml', '*.yml'])
95
            ->notName('*.template.*')
96
        ;
97
98
        return $files;
99
    }
100
101
    private function collectLocales(bool $includeRegions = true): void
102
    {
103
        $files = $this->getTranslationFiles();
104
        foreach ($files as $file) {
105
            $fileName = $file->getBasename($file->getExtension());
106
            if (false === mb_strpos($fileName, '.')) {
107
                continue;
108
            }
109
            list(, $locale) = explode('.', $fileName);
110
            if (!$includeRegions && false !== mb_strpos($locale, '_')) {
111
                $localeParts = explode('_', $locale);
112
                $locale = $localeParts[0];
113
            }
114
            if (!in_array($locale, $this->supportedLocales[$this->sectionKey], true)) {
115
                $this->supportedLocales[$this->sectionKey][] = $locale;
116
            }
117
        }
118
    }
119
}
120