This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelativeDateParser/src/enko/RelativeDateParser/DateTranslator.php
2014-09-30 07:05:21 +02:00

51 lines
1.6 KiB
PHP
Executable file

<?php
namespace enko\RelativeDateParser;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PoFileLoader;
class DateTranslator {
/** @var DateTranslator */
private static $instance = null;
private $translator = null;
public static function getInstance($lang = '') {
if (is_null(static::$instance)) {
static::$instance = new static($lang);
}
return static::$instance;
}
private function __construct($lang) {
if ($lang == '') {
throw new \BadMethodCallException('Language is missing');
}
$translations_path = realpath(__DIR__ . '/../../../translations');
$dir_iterator = new \RecursiveDirectoryIterator($translations_path);
$iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if (basename($file,'.po') == $lang) {
$loader = new PoFileLoader();
$loader->load(realpath($file),$lang);
$this->translator = new Translator($lang);
$this->translator->addLoader('pofile', $loader);
$this->translator->addResource('pofile',realpath($file),$lang);
}
}
if (is_null($this->translator)) {
throw new \BadMethodCallException('No translation file for this language available.');
}
}
public function translate($string,$params = []) {
return $this->translator->trans($string,$params);
}
}