1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework DateTime Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU Lesser General Public License version 2.1 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\DateTime\Translator; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Translation\MessageSelector; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Default implementation of AbstractTranslator. |
15
|
|
|
* |
16
|
|
|
* @since 2.0.0 |
17
|
|
|
*/ |
18
|
|
|
final class DateTimeTranslator extends AbstractTranslator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var MessageSelector |
22
|
|
|
* @since 2.0.0 |
23
|
|
|
*/ |
24
|
|
|
private $selector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
* @since 2.0.0 |
29
|
|
|
*/ |
30
|
|
|
private $loaded = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @since 2.0.0 |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct() |
38
|
|
|
{ |
39
|
1 |
|
$this->selector = new MessageSelector; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns a translated item. |
44
|
|
|
* |
45
|
|
|
* @param string $item The item to translate. |
46
|
|
|
* @param array $replace An replace array. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
* |
50
|
|
|
* @since 2.0.0 |
51
|
|
|
*/ |
52
|
145 |
|
public function get($item, array $replace = array()) |
53
|
|
|
{ |
54
|
145 |
|
$this->load(); |
55
|
|
|
|
56
|
145 |
|
if (!isset($this->loaded[$this->locale][$item])) |
57
|
145 |
|
{ |
58
|
1 |
|
return $item; |
59
|
|
|
} |
60
|
|
|
|
61
|
145 |
|
$line = $this->loaded[$this->locale][$item]; |
62
|
|
|
|
63
|
145 |
|
return $this->makeReplacements($line, $replace); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns a translated item with a proper form for pluralization. |
68
|
|
|
* |
69
|
|
|
* @param string $item The item to translate. |
70
|
|
|
* @param integer $number Number of items for pluralization. |
71
|
|
|
* @param array $replace An replace array. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
* |
75
|
|
|
* @since 2.0.0 |
76
|
|
|
*/ |
77
|
141 |
|
public function choice($item, $number, array $replace = array()) |
78
|
|
|
{ |
79
|
141 |
|
$lines = $this->get($item, $replace); |
80
|
|
|
|
81
|
141 |
|
$replace['count'] = $number; |
82
|
|
|
|
83
|
141 |
|
return $this->makeReplacements($this->selector->choose($lines, $number, $this->locale), $replace); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Loads dictionary. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* |
91
|
|
|
* @since 2.0.0 |
92
|
|
|
*/ |
93
|
145 |
|
private function load() |
94
|
|
|
{ |
95
|
145 |
|
if ($this->isLoaded()) |
96
|
145 |
|
{ |
97
|
144 |
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
$path = sprintf('%s/../../lang/%s.php', __DIR__, $this->locale); |
101
|
|
|
|
102
|
3 |
|
if (file_exists($path)) |
103
|
3 |
|
{ |
104
|
3 |
|
$this->loaded[$this->locale] = require $path; |
105
|
3 |
|
} |
106
|
3 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if a dictionary for the current locale is loaded. |
110
|
|
|
* |
111
|
|
|
* @return boolean |
112
|
|
|
* |
113
|
|
|
* @since 2.0.0 |
114
|
|
|
*/ |
115
|
145 |
|
private function isLoaded() |
116
|
|
|
{ |
117
|
145 |
|
return isset($this->loaded[$this->locale]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Replaces elements in a line. |
122
|
|
|
* |
123
|
|
|
* @param string $line The original line. |
124
|
|
|
* @param array $replace An replace array. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* |
128
|
|
|
* @since 2.0.0 |
129
|
|
|
*/ |
130
|
145 |
|
private function makeReplacements($line, array $replace) |
131
|
|
|
{ |
132
|
145 |
|
foreach ($replace as $key => $value) |
133
|
|
|
{ |
134
|
143 |
|
$line = str_replace(':' . $key, $value, $line); |
135
|
145 |
|
} |
136
|
|
|
|
137
|
145 |
|
return $line; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|