forked from cakephp/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslator.php
166 lines (149 loc) · 4.56 KB
/
Translator.php
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
159
160
161
162
163
164
165
166
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
*
* This file contains sections from the Aura Project
* @license https://github.com/auraphp/Aura.Intl/blob/3.x/LICENSE
*
* The Aura Project for PHP.
*
* @package Aura.Intl
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
namespace Cake\I18n;
use Aura\Intl\FormatterInterface;
use Aura\Intl\Package;
use Aura\Intl\TranslatorInterface;
/**
* Provides missing message behavior for CakePHP internal message formats.
*
* @internal
*/
class Translator implements TranslatorInterface
{
/**
* A fallback translator.
*
* @var \Aura\Intl\TranslatorInterface
*/
protected $fallback;
/**
* The formatter to use when translating messages.
*
* @var \Aura\Intl\FormatterInterface
*/
protected $formatter;
/**
* The locale being used for translations.
*
* @var string
*/
protected $locale;
/**
* The Package containing keys and translations.
*
* @var \Aura\Intl\Package
*/
protected $package;
/**
* Constructor
*
* @param string $locale The locale being used.
* @param \Aura\Intl\Package $package The Package containing keys and translations.
* @param \Aura\Intl\FormatterInterface $formatter A message formatter.
* @param \Aura\Intl\TranslatorInterface|null $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 string|bool 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 formatting any placeholders
*
*
* @param string $key The message key.
* @param array $tokensValues Token values to interpolate into the
* message.
* @return string The translated message with tokens replaced.
*/
public function translate($key, array $tokensValues = [])
{
$message = $this->getMessage($key);
if (!$message) {
// Fallback to the message key
$message = $key;
}
// Check for missing/invalid context
if (isset($message['_context'])) {
$context = isset($tokensValues['_context']) ? $tokensValues['_context'] : null;
unset($tokensValues['_context']);
// No or missing context, fallback to the key/first message
if ($context === null) {
$message = current($message['_context']);
} elseif (!isset($message['_context'][$context])) {
$message = $key;
} elseif (!isset($message['_context'][$context])) {
$message = $key;
} else {
$message = $message['_context'][$context];
}
}
if (!$tokensValues) {
// Fallback for plurals that were using the singular key
if (is_array($message)) {
return array_values($message + [''])[0];
}
return $message;
}
return $this->formatter->format($this->locale, $message, $tokensValues);
}
/**
* An object of type Package
*
* @return \Aura\Intl\Package
*/
public function getPackage()
{
return $this->package;
}
}