forked from jsmitka/Nette-Translation-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationPanel.php
197 lines (163 loc) · 4.81 KB
/
TranslationPanel.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?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.
*
*/
/*namespace Nette\Addons;*/
/*use Nette\Environment;*/
/*use Nette\Web\Html;*/
/**
* Panel for Nette DebugBar, which enables you to translate strings
* directly from your browser.
*
* @author Jan Smitka <[email protected]>
*/
class TranslationPanel implements /*\Nette\*/IDebugPanel
{
/* Layout constants */
const LAYOUT_HORIZONTAL = 1;
const LAYOUT_VERTICAL = 2;
/** @var IEditableTranslator */
protected $translator;
/** @var int TranslationPanel layout */
protected $layout = self::LAYOUT_HORIZONTAL;
/** @var int Height of the editor */
protected $height = 300;
public function __construct(IEditableTranslator $translator, $layout = NULL, $height = NULL)
{
$this->translator = $translator;
if ($height !== NULL) {
if (!is_numeric($height))
throw new /*\*/InvalidArgumentException('Panel height has to be a numeric value.');
$this->height = $height;
}
if ($layout !== NULL) {
$this->layout = $layout;
if ($height === NULL)
$this->height = 500;
}
Environment::getApplication()->onRequest[] = callback($this, 'processRequest');
}
/**
* Return's panel ID.
* @return string
*/
public function getId()
{
return 'TranslationPanel';
}
/**
* Returns the code for the panel tab.
* @return string
*/
public function getTab()
{
$tab = Html::el('span');
$image = $tab->create('img')->src($this->getIconSrc('flag_blue.png'))->id('TranslationPanel-icon');
$tab->add('Translations');
$tab->create('script')->type('text/javascript')->setHtml('translationPanel.init();');
return (string) $tab;
}
/**
* Returns the code for the panel itself.
* @return string
*/
public function getPanel()
{
$translator = $this->translator;
$strings = $this->translator->getStrings();
if (Environment::getSession()->isStarted()) {
$session = Environment::getSession('Nette.Addons.TranslationPanel');
$untranslatedStack = isset($session['stack']) ? $session['stack'] : array();
foreach ($strings as $string => $data) {
if (!$data) {
$untranslatedStack[$string] = FALSE;
}
}
$session['stack'] = $untranslatedStack;
foreach ($untranslatedStack as $string => $value) {
if (!isset($strings[$string]))
$strings[$string] = FALSE;
}
}
ob_start();
require dirname(__FILE__) . '/TranslationPanel.panel.phtml';
return ob_get_clean();
}
/**
* Handles an incomuing request and saves the data if necessary.
*/
public function processRequest()
{
// Try starting the session
try {
$session = Environment::getSession('Nette.Addons.TranslationPanel');
} catch (InvalidStateException $e) {
$session = FALSE;
}
$request = Environment::getHttpRequest();
if ($request->isPost() && $request->isAjax() && $request->getHeader('X-Translation-Client')) {
$data = json_decode(file_get_contents('php://input'));
if ($data) {
if ($session) {
$stack = isset($session['stack']) ? $session['stack'] : array();
}
foreach ($data as $string => $value) {
$this->translator->setTranslation($string, $value);
if ($session && isset($stack[$string]))
unset($stack[$string]);
}
$this->translator->save();
if ($session)
$session['stack'] = $stack;
}
}
}
/**
* Returns an embedded PNG icon.
* @param string $icon
* @return string
*/
protected function getIconSrc($icon)
{
return 'data:image/png;base64,' . base64_encode(file_get_contents(dirname(__FILE__) . '/icons/' . $icon));
}
/**
* Return an odrdinal number suffix.
* @param string $count
* @return string
*/
protected function ordinalSuffix($count)
{
switch (substr($count, -1)) {
case '1':
return 'st';
case '2':
return 'nd';
case '3':
return 'rd';
default:
return 'th';
}
}
}