auraphp /
Aura.Intl
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * |
||
| 4 | * This file is part of the Aura Project for PHP. |
||
| 5 | * |
||
| 6 | * @package aura/intl |
||
| 7 | * |
||
| 8 | * @license http://opensource.org/licenses/MIT MIT |
||
| 9 | * |
||
| 10 | */ |
||
| 11 | namespace Aura\Intl; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * |
||
| 15 | * Translator to translate the message |
||
| 16 | * |
||
| 17 | * @package aura/intl |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | class Translator implements TranslatorInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * |
||
| 24 | * A fallback translator. |
||
| 25 | * |
||
| 26 | * @var TranslatorInterface |
||
| 27 | * |
||
| 28 | */ |
||
| 29 | protected $fallback; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * The formatter to use when translating messages. |
||
| 34 | * |
||
| 35 | * @var FormatterInterface |
||
| 36 | * |
||
| 37 | */ |
||
| 38 | protected $formatter; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * The locale being used for translations. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | * |
||
| 46 | */ |
||
| 47 | protected $locale; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The Package containing keys and translations. |
||
| 51 | * |
||
| 52 | * @var Package |
||
| 53 | * |
||
| 54 | */ |
||
| 55 | protected $package; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | * Constructor |
||
| 60 | * |
||
| 61 | * @param string $locale The locale being used. |
||
| 62 | * |
||
| 63 | * @param Package $package The Package containing keys and translations. |
||
| 64 | * |
||
| 65 | * @param FormatterInterface $formatter A message formatter. |
||
| 66 | * |
||
| 67 | * @param TranslatorInterface $fallback A fallback translator. |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | 5 | public function __construct( |
|
| 71 | $locale, |
||
| 72 | Package $package, |
||
| 73 | FormatterInterface $formatter, |
||
| 74 | TranslatorInterface $fallback = null |
||
| 75 | ) { |
||
| 76 | 5 | $this->locale = $locale; |
|
| 77 | 5 | $this->package = $package; |
|
| 78 | 5 | $this->formatter = $formatter; |
|
| 79 | 5 | $this->fallback = $fallback; |
|
| 80 | 5 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * |
||
| 84 | * Gets the message translation by its key. |
||
| 85 | * |
||
| 86 | * @param string $key The message key. |
||
| 87 | * |
||
| 88 | * @return mixed The message translation string, or false if not found. |
||
| 89 | * |
||
| 90 | */ |
||
| 91 | 4 | protected function getMessage($key) |
|
| 92 | { |
||
| 93 | 4 | $message = $this->package->getMessage($key); |
|
| 94 | 4 | if ($message) { |
|
| 95 | 3 | return $message; |
|
| 96 | } |
||
| 97 | |||
| 98 | 3 | if ($this->fallback) { |
|
| 99 | // get the message from the fallback translator |
||
| 100 | 1 | $message = $this->fallback->getMessage($key); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 101 | 1 | if ($message) { |
|
| 102 | // speed optimization: retain locally |
||
| 103 | 1 | $this->package->addMessage($key, $message); |
|
| 104 | // done! |
||
| 105 | 1 | return $message; |
|
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // no local message, no fallback |
||
| 110 | 2 | return false; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * Translates the message indicated by they key, replacing token values |
||
| 116 | * along the way. |
||
| 117 | * |
||
| 118 | * @param string $key The message key. |
||
| 119 | * |
||
| 120 | * @param array $tokens_values Token values to interpolate into the |
||
| 121 | * message. |
||
| 122 | * |
||
| 123 | * @return string The translated message with tokens replaced. |
||
| 124 | * |
||
| 125 | */ |
||
| 126 | 4 | public function translate($key, array $tokens_values = []) |
|
| 127 | { |
||
| 128 | // get the message string |
||
| 129 | 4 | $message = $this->getMessage($key); |
|
| 130 | |||
| 131 | // do we have a message string? |
||
| 132 | 4 | if (! $message) { |
|
| 133 | // no, return the message key as-is |
||
| 134 | 2 | $message = $key; |
|
| 135 | } |
||
| 136 | |||
| 137 | // are there token replacement values? |
||
| 138 | 4 | if (empty($tokens_values)) { |
|
| 139 | // no, return the message string as-is |
||
| 140 | 3 | return $message; |
|
| 141 | } |
||
| 142 | |||
| 143 | // run message string through formatter to replace tokens with values |
||
| 144 | 2 | return $this->formatter->format($this->locale, $message, $tokens_values); |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * An object of type Package |
||
| 150 | * |
||
| 151 | * @return Package |
||
| 152 | * |
||
| 153 | */ |
||
| 154 | public function getPackage() |
||
| 155 | { |
||
| 156 | return $this->package; |
||
| 157 | } |
||
| 158 | } |
||
| 159 |