| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation\Traits; |
| 4 |
|
| 5 |
trait TranslationsTrait |
| 6 |
{ |
| 7 |
|
| 8 |
/** @var array */ |
| 9 |
protected $translations = []; |
| 10 |
|
| 11 |
/** |
| 12 |
* Given $key and $translation to set translation |
| 13 |
* |
| 14 |
* @param mixed $key |
| 15 |
* @param mixed $translation |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function setTranslation(string $key, string $translation) |
| 19 |
{ |
| 20 |
$this->translations[$key] = $translation; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Given $translations and set multiple translations |
| 25 |
* |
| 26 |
* @param array $translations |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function setTranslations(array $translations) |
| 30 |
{ |
| 31 |
$this->translations = array_merge($this->translations, $translations); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Given translation from given $key |
| 36 |
* |
| 37 |
* @param string $key |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function getTranslation(string $key): string |
| 41 |
{ |
| 42 |
return array_key_exists($key, $this->translations) ? $this->translations[$key] : $key; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get all $translations |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public function getTranslations(): array |
| 51 |
{ |
| 52 |
return $this->translations; |
| 53 |
} |
| 54 |
} |
| 55 |
|