first commit
This commit is contained in:
35
src/Services/TranslationService.php
Executable file
35
src/Services/TranslationService.php
Executable file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Symfony\Component\Translation\Loader\JsonFileLoader;
|
||||
|
||||
class TranslationService
|
||||
{
|
||||
private string $langDir;
|
||||
private array $supportedLocales;
|
||||
private string $defaultLocale;
|
||||
|
||||
public function __construct(string $langDir, array $supportedLocales, string $defaultLocale)
|
||||
{
|
||||
$this->langDir = $langDir;
|
||||
$this->supportedLocales = $supportedLocales;
|
||||
$this->defaultLocale = $defaultLocale;
|
||||
}
|
||||
|
||||
public function getTranslator(string $locale): Translator
|
||||
{
|
||||
$translator = new Translator($locale);
|
||||
$translator->addLoader('json', new JsonFileLoader());
|
||||
|
||||
foreach ($this->supportedLocales as $supportedLocale) {
|
||||
$filePath = $this->langDir . '/' . $supportedLocale . '.json';
|
||||
if (file_exists($filePath)) {
|
||||
$translator->addResource('json', $filePath, $supportedLocale);
|
||||
}
|
||||
}
|
||||
|
||||
return $translator;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user