Created
August 13, 2019 10:05
-
-
Save kobus1998/b4ca8b5c036b7e673bc8913847834c8a to your computer and use it in GitHub Desktop.
php translator
This file contains hidden or 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 | |
class TranslationMissing extends \Exception | |
{ | |
public function __construct($lang, $trans) | |
{ | |
parent::__construct("translation missing of $trans in language $lang"); | |
} | |
} | |
class FailedToLoad extends \Exception | |
{ | |
public function __construct(string $strategy) | |
{ | |
parent::__construct("Failed to load $strategy"); | |
} | |
} | |
class Translation | |
{ | |
/** @var string */ | |
public $lang; | |
/** @var string */ | |
public $key; | |
/** @var string */ | |
public $value; | |
/** | |
* @param string $lang | |
* @param string $key | |
* @param string $value | |
*/ | |
public function __construct($lang, $key, $value) | |
{ | |
$this->lang = $lang; | |
$this->key = $key; | |
$this->value = $value; | |
} | |
} | |
/** | |
* Load strategy | |
*/ | |
interface TranslationLoadStrategy | |
{ | |
/** | |
* @param string $lang | |
* @param mixed $resource | |
* @return array | |
* @throws FailedToLoad | |
*/ | |
public function load($lang, $resource): array; | |
} | |
class TranslationArrayLoader implements TranslationLoadStrategy | |
{ | |
/** | |
* @param string $lang | |
* @param mixed $resource | |
* @return array | |
* @throws FailedToLoad | |
*/ | |
public function load($lang, $resource): array | |
{ | |
if (!is_array($resource)) { | |
throw new FailedToLoad('TranslationArrayLoader'); | |
} | |
$result = []; | |
foreach($resource as $key => $value) | |
{ | |
$result[] = new Translation($lang, $key, $value); | |
} | |
return $result; | |
} | |
} | |
class Translator | |
{ | |
/** @var array */ | |
protected $translations = []; | |
/** | |
* @param string $lang | |
*/ | |
public function __construct($lang) | |
{ | |
$this->lang = $lang; | |
} | |
/** | |
* translate | |
* | |
* @param string $key | |
* @return string | |
* @throws TranslationMissing | |
*/ | |
public function trans($key) | |
{ | |
$key = strtolower($key); | |
if (!isset($this->translations[$key])) { | |
throw new TranslationMissing($this->lang, $key); | |
} | |
return $this->translations[$key]; | |
} | |
/** | |
* add translation | |
* | |
* @param Translation $translation | |
* @return self | |
*/ | |
public function add(Translation $translation) | |
{ | |
if ($this->lang != $translation->lang) { | |
return $this; | |
} | |
$this->translations[strtolower($translation->key)] = $translation->value; | |
return $this; | |
} | |
/** | |
* load by load strategy | |
* | |
* @param mixed $resource | |
* @param TranslationLoadStrategy $strategy | |
* @return self | |
*/ | |
public function load($resource, TranslationLoadStrategy $strategy) | |
{ | |
foreach($strategy->load($this->lang, $resource) as $translation) { | |
$this->add($translation); | |
} | |
return $this; | |
} | |
} | |
$lang = 'nl'; | |
$data = [ | |
'Dog' => 'hond', | |
'cat' => 'kat', | |
'fridge' => 'koelkast' | |
]; | |
$translator = new Translator($lang); | |
$translator->load($data, new TranslationArrayLoader()); | |
echo $translator->trans('dog'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment