1
|
|
|
<?php |
2
|
|
|
namespace l10nNetteTranslator; |
3
|
|
|
|
4
|
|
|
use Nette\Application\Responses\JsonResponse; |
5
|
|
|
use Nette\Http\IRequest; |
6
|
|
|
use Nette\InvalidStateException; |
7
|
|
|
use Nette\Object; |
8
|
|
|
|
9
|
|
|
class TranslatorProcessor extends Object { |
10
|
|
|
const PARAMETER = 'l10nNTP'; |
11
|
|
|
|
12
|
|
|
/** @var \Nette\Http\IRequest */ |
13
|
|
|
private $request; |
14
|
|
|
|
15
|
|
|
/** @var \l10nNetteTranslator\Translator */ |
16
|
|
|
private $translator; |
17
|
|
|
|
18
|
|
|
private $payload = [ |
19
|
|
|
'actions' => [], |
20
|
|
|
'language' => null, |
21
|
|
|
'languages' => [], |
22
|
|
|
'texts' => [], |
23
|
|
|
'select' => null, |
24
|
|
|
'message' => null, |
25
|
|
|
]; |
26
|
|
|
|
27
|
8 |
|
public function __construct(Translator $translator, IRequest $request) { |
28
|
8 |
|
$this->translator = $translator; |
29
|
8 |
|
$this->request = $request; |
30
|
8 |
|
} |
31
|
|
|
|
32
|
3 |
|
protected function createHash($value) { |
33
|
3 |
|
return hash('crc32b', self::PARAMETER . '-' . $value); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
protected function getRequestData() { |
37
|
1 |
|
$request_data = (array)$this->request->getPost(self::PARAMETER); |
38
|
|
|
$request_data += [ |
39
|
1 |
|
'action' => null, |
40
|
1 |
|
'key' => null, |
41
|
1 |
|
'language' => null, |
42
|
1 |
|
'texts' => [] |
43
|
1 |
|
]; |
44
|
|
|
|
45
|
1 |
|
if ($request_data['key'] && empty($request_data['id'])) { |
46
|
1 |
|
$request_data['id'] = $this->createHash($request_data['key']); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
return $request_data; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
protected function getPayload() { |
53
|
3 |
|
return $this->payload; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
protected function initAction() { |
57
|
1 |
|
$this->loadLanguagesAction(); |
58
|
1 |
|
$this->loadListAction(); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
protected function loadLanguagesAction() { |
62
|
1 |
|
foreach ($this->translator->getLanguagesAndPlurals() as $language_and_plural) { |
63
|
1 |
|
$language = $language_and_plural->getLanguage(); |
64
|
1 |
|
$code = $language->getIso639_1(); |
65
|
|
|
|
66
|
1 |
|
$this->payload['languages'][$code] = [ |
67
|
1 |
|
'code' => $code, |
68
|
1 |
|
'original_name' => $language->getOriginalName(), |
69
|
1 |
|
'english_name' => $language->getEnglishName() |
70
|
1 |
|
]; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
$active_language_and_plural = $this->translator->getActiveLanguageAndPlural(); |
74
|
|
|
|
75
|
1 |
|
$this->payload['language'] = $active_language_and_plural->getLanguage()->getIso639_1(); |
76
|
1 |
|
$this->payload['plurals_count'] = $active_language_and_plural->getPlural()->getPluralsCount(); |
77
|
1 |
|
$this->payload['actions'][] = 'buildLanguages'; |
78
|
1 |
|
$this->payload['actions'][] = 'setLanguage'; |
79
|
1 |
|
$this->payload['actions'][] = 'buildPluralsForm'; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
protected function loadListAction() { |
83
|
1 |
|
$translator = $this->translator->getTranslator(); |
84
|
1 |
|
$untranslated = $translator->getUntranslated(); |
85
|
1 |
|
$translated = $translator->getTranslated(); |
86
|
|
|
|
87
|
1 |
|
$keys = array_keys($translated + $untranslated); |
88
|
1 |
|
natsort($keys); |
89
|
|
|
|
90
|
1 |
|
foreach ($keys as $key) { |
91
|
1 |
|
$hash = $this->createHash($key); |
92
|
1 |
|
$this->payload['texts'][$hash] = [ |
93
|
1 |
|
'id' => $hash, |
94
|
1 |
|
'key' => $key, |
95
|
1 |
|
'status' => (int)!isset($untranslated[$key]), |
96
|
1 |
|
'texts' => isset($translated[$key]) ? $translated[$key] : [] |
97
|
1 |
|
]; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
$this->payload['actions'][] = 'buildList'; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
protected function saveTextAction(array $request_data) { |
104
|
1 |
|
if ($request_data['texts']) { |
105
|
1 |
|
$translator = $this->translator->getTranslator(); |
106
|
1 |
|
$translator->removeText($request_data['key']); |
107
|
1 |
|
$active_language_and_plural = $this->translator->getActiveLanguageAndPlural(); |
108
|
1 |
|
$plurals_count = $active_language_and_plural->getPlural()->getPluralsCount(); |
109
|
|
|
|
110
|
1 |
|
for ($plural = 0; $plural < $plurals_count; $plural += 1) { |
111
|
1 |
|
if (isset($request_data['texts'][$plural]) && $request_data['texts'][$plural] != '') { |
112
|
1 |
|
$translator->setText($request_data['key'], $request_data['texts'][$plural], $plural); |
113
|
1 |
|
} |
114
|
1 |
|
} |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
$this->loadListAction(); |
118
|
1 |
|
$this->payload['select'] = $this->createHash($request_data['key']); |
119
|
1 |
|
$this->payload['actions'][] = 'selectItem'; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
protected function removeTextAction(array $request_data) { |
123
|
1 |
|
$translator = $this->translator->getTranslator(); |
124
|
1 |
|
$translator->removeText($request_data['key']); |
125
|
|
|
|
126
|
1 |
|
$this->loadListAction(); |
127
|
1 |
|
$this->payload['actions'][] = 'clean'; |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
protected function callActionByRequest(array $request_data) { |
131
|
1 |
|
$action = $request_data['action'] . 'Action'; |
132
|
|
|
|
133
|
1 |
|
if (!method_exists($this, $action)) { |
134
|
1 |
|
throw new InvalidStateException(sprintf('Action "%s" not found', $request_data['action'])); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
call_user_func([$this, $action], $request_data); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
5 |
|
public function run() { |
141
|
5 |
|
$request_data = $this->getRequestData(); |
142
|
|
|
|
143
|
5 |
|
if ($request_data['action']) { |
144
|
|
|
try { |
145
|
4 |
|
if ($request_data['language']) { |
146
|
2 |
|
$this->translator->setActiveLanguageCode($request_data['language']); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
3 |
|
$this->callActionByRequest($request_data); |
150
|
|
|
} |
151
|
4 |
|
catch (InvalidStateException $e) { |
152
|
2 |
|
$this->payload['message'] = $e->getMessage(); |
153
|
|
|
} |
154
|
|
|
|
155
|
4 |
|
return new JsonResponse($this->payload); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return null; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|