Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| LanguageTranslator | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| getInstance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setJsonFileLoader | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| setPoFileLoader | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Slim; |
| 4 | |
| 5 | // Symfony Translation Classes |
| 6 | use Symfony\Component\Translation\Loader\JsonFileLoader; |
| 7 | use Symfony\Component\Translation\Loader\PoFileLoader; |
| 8 | use Symfony\Component\Translation\Translator; |
| 9 | |
| 10 | class LanguageTranslator |
| 11 | { |
| 12 | protected Translator $translator; |
| 13 | |
| 14 | protected string $defaultLang; |
| 15 | |
| 16 | /** @var array{json: string, pofile: string} */ |
| 17 | protected array $loaderTypes = [ |
| 18 | 'pofile' => 'setPoFileLoader', |
| 19 | 'json' => 'setJsonFileLoader' |
| 20 | ]; |
| 21 | |
| 22 | public function __construct(string $fallbackLocale, string $defaultLocale, string $defaultLang) |
| 23 | { |
| 24 | $translatorType = (\App::$languagesource) ? \App::$languagesource : 'pofile'; |
| 25 | |
| 26 | $this->defaultLang = $defaultLang; |
| 27 | |
| 28 | /** @var class-string<Translator> $translatorClass */ |
| 29 | $translatorClass = \App::TRANSLATOR_CLASS; |
| 30 | // First param is the "default language" to use. |
| 31 | /** @psalm-suppress UnsafeInstantiation */ |
| 32 | $this->translator = new $translatorClass($defaultLocale); |
| 33 | // Set a fallback language incase you don't have a translation in the default language |
| 34 | $this->translator->setFallbackLocales([$fallbackLocale]); |
| 35 | // Add a loader that will get the php files we are going to store our translations in |
| 36 | $initLoader = $this->loaderTypes[$translatorType] ?? 'setPoFileLoader'; |
| 37 | $this->$initLoader(); |
| 38 | } |
| 39 | |
| 40 | public function getInstance(): Translator |
| 41 | { |
| 42 | return $this->translator; |
| 43 | } |
| 44 | |
| 45 | /** @psalm-api */ |
| 46 | protected function setJsonFileLoader(): void |
| 47 | { |
| 48 | $this->translator->addLoader('json', new JsonFileLoader()); |
| 49 | foreach (\App::$supportedLanguages as $language) { |
| 50 | $this->translator->addResource( |
| 51 | 'json', |
| 52 | \App::APP_PATH . '/lang/' . $language['locale'] . '.json', |
| 53 | $language['locale'] |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** @psalm-api */ |
| 59 | protected function setPoFileLoader(): void |
| 60 | { |
| 61 | $this->translator->addLoader('pofile', new PoFileLoader()); |
| 62 | foreach (\App::$supportedLanguages as $locale => $language) { |
| 63 | if ($locale != $this->defaultLang) { |
| 64 | $this->translator->addResource( |
| 65 | 'pofile', |
| 66 | \App::APP_PATH . '/lang/' . $language['locale'] . '.po', |
| 67 | $language['locale'] |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |