-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonLocale.php
183 lines (156 loc) · 4.52 KB
/
JsonLocale.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* Copyright (c) 2010 Jan Smitka <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* Represents the set of localisation files.
*
* @author Jan Smitka <[email protected]>
*/
class JsonLocale extends Object implements IEditableTranslator
{
/** @var string */
private $locale;
/** @var string */
private $name;
/** @var array */
private $plurals = array(0 => 1, 1 => 0, 2 => 1);
/** @var string */
private $strings = array();
/** @var string */
private $directory;
/** @var string */
private $currentSheet = 'main';
/** @var array */
private $updatedSheets = array();
public function __construct($directory, $locale)
{
$this->locale = $locale;
$this->directory = $directory;
if (!file_exists($file = $directory . '/' . $locale . '.json'))
throw new FileNotFoundException('File \'' . $file . '\' does not exists.');
$info = json_decode(file_get_contents($file));
if (isset($info->name))
$this->name = $info->name;
if (isset($info->plurals))
$this->plurals = (array) $info->plurals;
if (isset($info->defaultSheet))
$this->currentSheet = $info->defaultSheet;
unset($info);
// TODO: add support for locale inheritance
}
/**
* Translates the $message. Printf-like formatting is allowed.
* @param string $message
* @param int $count
* @return string
*/
public function translate($message, $count = NULL)
{
$string = $this->getString($message);
if (is_array($string)) {
$n = 0;
foreach ($this->plurals as $c => $i)
if (abs((int) $count) >= $c)
$n = $i;
if (isset($string[$n]))
$string = $string[$n];
else
$string = $string[0];
}
$args = func_get_args();
array_shift($args);
if (!empty($args))
return vsprintf($string, $args);
else
return $string;
}
public function getString($string, $default = FALSE)
{
if (strpos($string, '/') !== FALSE)
list($sheet, $string) = explode('/', $string, 2);
else
$sheet = $this->currentSheet;
$sheet = $this->getSheet($sheet);
if (isset($sheet->$string) && $sheet->$string)
return $sheet->$string;
else {
if ($string)
$sheet->$string = FALSE;
return $default !== FALSE ? $default : $string;
}
}
public function getCurrentSheet()
{
return $this->currentSheet;
}
public function setCurrentSheet($currentSheet)
{
$this->currentSheet = $currentSheet;
}
public function getSheet($sheet)
{
if (!isset($this->strings[$sheet]))
$this->loadSheet($sheet);
return $this->strings[$sheet];
}
private function loadSheet($sheet)
{
if (file_exists($file = $this->directory . '/' . $sheet . '.json')) {
$this->strings[$sheet] = json_decode(file_get_contents($file));
} else {
$this->strings[$sheet] = (object) array();
}
}
public function getVariantsCount()
{
return count(array_unique($this->plurals));
}
public function getStrings()
{
$stringList = array();
foreach ($this->strings as $sheet => $strings) {
foreach ($strings as $key => $string)
$stringList[$sheet . '/' . $key] = $string;
}
ksort($stringList);
return $stringList;
}
public function setTranslation($string, $value)
{
if (strpos($string, '/') !== FALSE)
list($sheet, $string) = explode('/', $string, 2);
else
$sheet = $this->currentSheet;
$this->updatedSheets[$sheet] = $this->getSheet($sheet);
$sheet = $this->getSheet($sheet);
$sheet->$string = $value;
}
public function save()
{
foreach ($this->updatedSheets as $sheet => $strings) {
file_put_contents($this->directory . '/' . $sheet . '.json', json_encode($strings));
}
}
}