Created
February 19, 2015 00:04
-
-
Save CarsonF/38efd606726268855e36 to your computer and use it in GitHub Desktop.
Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Service\Translation; | |
use Common\Config; | |
use Common\Log; | |
use GMO\Common\Collections\ArrayCollection; | |
use GMO\Common\String; | |
use Pimple; | |
use Symfony\Component\Finder\Finder; | |
use Symfony\Component\Translation\Translator as BaseTranslator; | |
use Symfony\Component\Translation\MessageSelector; | |
/** | |
* Translator can extend Silex's once this pr is merged. | |
* https://github.com/silexphp/Silex/pull/1090 | |
*/ | |
class Translator extends BaseTranslator { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getLocale() { | |
return $this->container['locale']; | |
} | |
public function setLocale($locale) { | |
if ($locale === null) { | |
return; | |
} | |
parent::setLocale($locale); | |
$this->container['locale'] = $locale; | |
} | |
/** | |
* Caches non fresh catalogues for locale provided | |
* or all locales we have translations for | |
* @param string $localeToCache | |
*/ | |
public function warmCache($localeToCache = null) { | |
$locales = $localeToCache ? array($localeToCache) : $this->getAllSupportedLocales(); | |
foreach ($locales as $locale) { | |
$this->loadCatalogue($locale); | |
} | |
} | |
protected function initializeCatalogue($locale) { | |
$genericLocale = String::splitFirst($locale, '_'); | |
foreach ($this->getFilesForLocale($genericLocale) as $file) { | |
$this->logger->debug('translation file added: ' . $file); | |
$this->addResource('yaml', (string) $file, $genericLocale); | |
} | |
foreach ($this->getFallbackLocales() as $fallbackLocale) { | |
foreach ($this->getFilesForLocale($fallbackLocale) as $file) { | |
$this->logger->debug('fallback translation file added: ' . $file); | |
$this->addResource('yaml', (string) $file, $fallbackLocale); | |
} | |
} | |
/* | |
* Needed until this pr is merged | |
* https://github.com/silexphp/Silex/pull/1086 | |
*/ | |
$this->addComponentResource('Symfony\Component\Validator\Validator', $locale); | |
$this->addComponentResource('Symfony\Component\Form\Form', $locale); | |
parent::initializeCatalogue($locale); | |
} | |
protected function getAllSupportedLocales() { | |
return ArrayCollection::create( | |
Finder::create() | |
->files() | |
->in(Config::absPath('translations')) | |
->exclude(Config::getTranslationCacheDir()) | |
->name("*.yml") | |
->getIterator() | |
)->map(function($file) { | |
preg_match('#.+\\.(.+)\\.yml#', $file, $matches); | |
return $matches[1]; | |
})->unique(); | |
} | |
protected function getFilesForLocale($locale) { | |
return Finder::create() | |
->files() | |
->in(Config::absPath('translations')) | |
->exclude(Config::getTranslationCacheDir()) | |
->name("*.$locale.yml"); | |
} | |
private function addComponentResource($cls, $locale) { | |
$r = new \ReflectionClass($cls); | |
$file = dirname($r->getFilename()) . '/Resources/translations/validators.' . $locale . '.xlf'; | |
if (file_exists($file)) { | |
$this->addResource('xliff', $file, $locale); | |
} | |
} | |
public function __construct(Pimple $container, MessageSelector $selector, $cacheDir = null, $debug = false) { | |
$this->container = $container; | |
$this->logger = Log::create()->getLogger(); | |
parent::__construct(null, $selector, $cacheDir, $debug); | |
} | |
protected $container; | |
/** @var \Psr\Log\LoggerInterface */ | |
protected $logger; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment