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