|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +namespace Sitegeist\CsvLabels\Translation\Loader; |
| 4 | + |
| 5 | +use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; |
| 6 | +use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 7 | +use Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 8 | +use Symfony\Component\Translation\Loader\LoaderInterface; |
| 9 | +use Symfony\Component\Translation\MessageCatalogue; |
| 10 | +use TYPO3\CMS\Core\Localization\Locales; |
| 11 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 12 | + |
| 13 | +#[Autoconfigure(tags: [['name' => 'translation.loader', 'format' => 'csv']])] |
| 14 | +class CsvLoader implements LoaderInterface |
| 15 | +{ |
| 16 | + private ?Locales $locales = null; |
| 17 | + |
| 18 | + public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue |
| 19 | + { |
| 20 | + if (!file_exists($resource)) { |
| 21 | + throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource), 1612282091); |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + $file = new \SplFileObject($resource, 'r'); |
| 26 | + $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); |
| 27 | + } catch (\RuntimeException $e) { |
| 28 | + throw new InvalidResourceException(\sprintf('File "%s" could not be opened.', $resource), 1767019766, $e); |
| 29 | + } |
| 30 | + |
| 31 | + if (!$file->valid()) { |
| 32 | + throw new InvalidResourceException(\sprintf('File "%s" has invalid CSV content.', $resource), 1767623886); |
| 33 | + } |
| 34 | + |
| 35 | + $csvHeader = $file->current(); |
| 36 | + if ($csvHeader === false) { |
| 37 | + return new MessageCatalogue($locale); |
| 38 | + } |
| 39 | + |
| 40 | + $this->locales = GeneralUtility::makeInstance(Locales::class); |
| 41 | + $localeColumnIndex = $this->searchLocaleColumnIndex($csvHeader, $locale); |
| 42 | + if ($localeColumnIndex === false) { |
| 43 | + return new MessageCatalogue($locale); |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($file as $lineNumber => $translation) { |
| 47 | + if ($lineNumber === 0 || empty($translation[0])) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $identifier = $translation[0]; |
| 51 | + if (isset($translation[$localeColumnIndex]) && $translation[$localeColumnIndex] !== '') { |
| 52 | + $messages[$identifier] = $translation[$localeColumnIndex]; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return new MessageCatalogue($locale, [$domain => $messages]); |
| 57 | + } |
| 58 | + |
| 59 | + private function searchLocaleColumnIndex(array $csvHeader, string $locale): int|false |
| 60 | + { |
| 61 | + $localeChain = $this->buildLocaleFallbackChain($locale, $this->locales); |
| 62 | + |
| 63 | + foreach ($localeChain as $currentLocale) { |
| 64 | + $index = array_search($currentLocale, $csvHeader, true); |
| 65 | + if ($index !== false) { |
| 66 | + return $index; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + private function buildLocaleFallbackChain(string $locale, Locales $locales): array |
| 74 | + { |
| 75 | + $chain = [$locale]; |
| 76 | + |
| 77 | + // e.g. de-DE -> de |
| 78 | + if (str_contains($locale, '-')) { |
| 79 | + [$baseLanguage] = explode('-', $locale, 2); |
| 80 | + $chain[] = $baseLanguage; |
| 81 | + } elseif (str_contains($locale, '_')) { |
| 82 | + [$baseLanguage] = explode('_', $locale, 2); |
| 83 | + $chain[] = $baseLanguage; |
| 84 | + } |
| 85 | + |
| 86 | + $chain = array_merge($chain, $this->locales->getLocaleDependencies($locale)); |
| 87 | + |
| 88 | + $chain[] = 'default'; |
| 89 | + |
| 90 | + return array_unique($chain); |
| 91 | + } |
| 92 | +} |
0 commit comments