Skip to content
Snippets Groups Projects
Commit 76a04ac4 authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Rewrite Localization using symfony/filesystem

parent b326e5b3
No related branches found
No related tags found
No related merge requests found
...@@ -10,10 +10,13 @@ declare(strict_types=1); ...@@ -10,10 +10,13 @@ declare(strict_types=1);
namespace SimpleSAML\Locale; namespace SimpleSAML\Locale;
use Exception;
use Gettext\Translations; use Gettext\Translations;
use Gettext\Translator; use Gettext\Translator;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Logger; use SimpleSAML\Logger;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
class Localization class Localization
{ {
...@@ -66,6 +69,11 @@ class Localization ...@@ -66,6 +69,11 @@ class Localization
*/ */
private string $langcode; private string $langcode;
/**
* @var \Symfony\Component\Filesystem\Filesystem;
*/
private Filesystem $fileSystem;
/** /**
* Constructor * Constructor
...@@ -74,6 +82,7 @@ class Localization ...@@ -74,6 +82,7 @@ class Localization
*/ */
public function __construct(Configuration $configuration) public function __construct(Configuration $configuration)
{ {
$this->fileSystem = new Filesystem();
$this->configuration = $configuration; $this->configuration = $configuration;
/** @var string $locales */ /** @var string $locales */
$locales = $this->configuration->resolvePath('locales'); $locales = $this->configuration->resolvePath('locales');
...@@ -186,7 +195,7 @@ class Localization ...@@ -186,7 +195,7 @@ class Localization
// Locale for default language missing even, error out // Locale for default language missing even, error out
$error = "Localization directory '$langPath' missing/broken for langcode '$langcode' and domain '$domain'"; $error = "Localization directory '$langPath' missing/broken for langcode '$langcode' and domain '$domain'";
Logger::critical($_SERVER['PHP_SELF'] . ' - ' . $error); Logger::critical($_SERVER['PHP_SELF'] . ' - ' . $error);
throw new \Exception($error); throw new Exception($error);
} }
...@@ -217,7 +226,7 @@ class Localization ...@@ -217,7 +226,7 @@ class Localization
): void { ): void {
try { try {
$langPath = $this->getLangPath($domain); $langPath = $this->getLangPath($domain);
} catch (\Exception $e) { } catch (Exception $e) {
$error = "Something went wrong when trying to get path to language file, cannot load domain '$domain'."; $error = "Something went wrong when trying to get path to language file, cannot load domain '$domain'.";
Logger::debug($_SERVER['PHP_SELF'] . ' - ' . $error); Logger::debug($_SERVER['PHP_SELF'] . ' - ' . $error);
if ($catchException) { if ($catchException) {
...@@ -227,14 +236,18 @@ class Localization ...@@ -227,14 +236,18 @@ class Localization
throw $e; throw $e;
} }
} }
$poFile = $domain . '.po';
$poPath = $langPath . $poFile; $file = new File($langPath . $domain . '.po');
if (file_exists($poPath) && is_readable($poPath)) { if ($this->fileSystem->exists($file->getRealPath()) && $file->isReadable()) {
$translations = Translations::fromPoFile($poPath); $translations = Translations::fromPoFile($file->getRealPath());
$this->translator->loadTranslations($translations); $this->translator->loadTranslations($translations);
} else { } else {
$error = "Localization file '$poFile' not found in '$langPath', falling back to default"; Logger::debug(sprintf(
Logger::debug($_SERVER['PHP_SELF'] . ' - ' . $error); "%s - Localization file '%s' not found or not readable in '%s', falling back to default",
$_SERVER['PHP_SELF'],
$file->getfileName(),
$langPath,
));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment