Created
September 17, 2015 10:23
-
-
Save feelinc/d9af4d34ccbe97f9cf09 to your computer and use it in GitHub Desktop.
Translation class similar to Gettext usage approach
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 | |
use Symfony\Component\Translation\Translator as SymfonyTranslator; | |
use Symfony\Component\Translation\MessageSelector; | |
use Symfony\Component\Translation\Loader\PhpFileLoader; | |
class Translator | |
{ | |
/** | |
* The loader implementation. | |
* | |
* @var \Symfony\Component\Translation\Loader\LoaderInterface | |
*/ | |
protected $loader; | |
/** | |
* The default locale being used by the translator. | |
* | |
* @var string | |
*/ | |
protected $locale; | |
/** | |
* The default locale path being used by the translator. | |
* | |
* @var string | |
*/ | |
protected $localePath; | |
/** | |
* The translator object. | |
* | |
* @var \Symfony\Component\Translation\TranslatorInterface | |
*/ | |
protected $trans; | |
/** | |
* Create a new instance. | |
* | |
* @param string $locale | |
* @param string $path | |
* @return void | |
*/ | |
public function __construct($locale = '', $path = '') | |
{ | |
$this->createLoader(); | |
return $this->setLocale($locale, $path); | |
} | |
/** | |
* Set current locale. | |
* | |
* @param string $locale | |
* @param string $path | |
* @param string $domain | |
* @return self | |
*/ | |
public function setLocale($locale, $path = '', $domain = 'messages') | |
{ | |
$this->locale = $locale; | |
if ( ! empty($path)) { | |
$this->localePath = $path; | |
} | |
$this->domain = $domain; | |
// path to the .php file that we should monitor | |
$file = $this->localePath.'/'.$locale.'/'.$this->domain.'.php'; | |
$fallbackLocale = substr($this->locale, 0, 2); | |
$this->trans = $this->createTranslator($this->locale); | |
$this->trans->setFallbackLocales([$fallbackLocale]); | |
$this->trans->addLoader('php', $this->loader); | |
$this->trans->addResource('php', $file, $this->locale, $this->domain); | |
return $this; | |
} | |
/** | |
* Translate a text. | |
* | |
* @param string $text | |
* @param mixed $parameters | |
* @param string $domain | |
* @return string | |
*/ | |
public function trans($text, $parameters = [], $domain = 'messages') | |
{ | |
if ( ! is_array($parameters)) { | |
$domain = $parameters; | |
$parameters = []; | |
} | |
if ($domain !== $this->domain) { | |
$this->setLocale($this->locale, $this->localePath, $domain); | |
} | |
return $this->trans->trans($text, $parameters, $this->domain); | |
} | |
/** | |
* Create the translator object. | |
* | |
* @param string $locale | |
* @return \Symfony\Component\Translation\TranslatorInterface | |
*/ | |
private function createTranslator($locale) | |
{ | |
return new SymfonyTranslator($locale, new MessageSelector()); | |
} | |
/** | |
* Get the loader object. | |
* | |
* @return \Symfony\Component\Translation\Loader\LoaderInterface | |
*/ | |
private function createLoader() | |
{ | |
return $this->loader = new PhpFileLoader; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment