|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\StudioBackendBundle\Translation\Service; |
| 15 | + |
| 16 | +use Pimcore\Localization\LocaleServiceInterface; |
| 17 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +final readonly class AdminLanguageService implements AdminLanguageServiceInterface |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private string $translationsPath, |
| 26 | + private string $defaultTranslationsPath, |
| 27 | + private KernelInterface $kernel, |
| 28 | + private LocaleServiceInterface $localeService, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function getAvailableAdminLanguages(): array |
| 33 | + { |
| 34 | + $translatedLanguages = []; |
| 35 | + |
| 36 | + foreach ($this->getLanguageDirectories() as $directory) { |
| 37 | + $translatedLanguages = array_merge($translatedLanguages, $this->scanDirectoryForLanguages($directory)); |
| 38 | + } |
| 39 | + |
| 40 | + return array_unique($translatedLanguages); |
| 41 | + } |
| 42 | + |
| 43 | + private function getLanguageDirectories(): array |
| 44 | + { |
| 45 | + $directories = []; |
| 46 | + |
| 47 | + $languageDir = $this->kernel->locateResource($this->translationsPath); |
| 48 | + if (is_dir($languageDir)) { |
| 49 | + $directories[] = $languageDir; |
| 50 | + } |
| 51 | + |
| 52 | + if (is_dir($this->defaultTranslationsPath)) { |
| 53 | + $directories[] = $this->defaultTranslationsPath; |
| 54 | + } |
| 55 | + |
| 56 | + return $directories; |
| 57 | + } |
| 58 | + |
| 59 | + private function scanDirectoryForLanguages(string $directory): array |
| 60 | + { |
| 61 | + $languages = []; |
| 62 | + $files = scandir($directory); |
| 63 | + |
| 64 | + if ($files === false) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($files as $file) { |
| 69 | + $languageCode = $this->extractLanguageCode($directory, $file); |
| 70 | + if ($languageCode !== null) { |
| 71 | + $languages[] = $languageCode; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $languages; |
| 76 | + } |
| 77 | + |
| 78 | + private function extractLanguageCode(string $directory, string $file): ?string |
| 79 | + { |
| 80 | + if (!is_file($directory . '/' . $file)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $parts = explode('.', $file); |
| 85 | + if (count($parts) < 2) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $languageCode = $parts[0]; |
| 90 | + if ($parts[0] === 'studio' && isset($parts[1])) { |
| 91 | + $languageCode = $parts[1]; |
| 92 | + } |
| 93 | + |
| 94 | + $extension = end($parts); |
| 95 | + if (($extension === 'json' || $parts[0] === 'studio') && $this->localeService->isLocale($languageCode)) { |
| 96 | + return $languageCode; |
| 97 | + } |
| 98 | + |
| 99 | + return null; |
| 100 | + } |
| 101 | +} |
0 commit comments