/
translate_interface.php
239 lines (223 loc) · 10.4 KB
/
translate_interface.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Multilingual;
use Concrete\Core\Application\EditResponse;
use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Multilingual\Page\Section\Translation;
use Concrete\Core\Page\Controller\DashboardSitePageController;
use Core;
use Database;
use Config;
defined('C5_EXECUTE') or die("Access Denied.");
class TranslateInterface extends DashboardSitePageController
{
public $helpers = array('form');
public function view()
{
$extractor = Core::make('multilingual/extractor');
$this->set('extractor', $extractor);
$this->set('site', $this->getSite());
$this->set('app', $this->app);
}
public function reloaded()
{
$this->set('message', t('Languages refreshed.'));
$this->view();
}
public function exported()
{
$this->set('message', t('The translations have been exported to file and will be used by the website.'));
$this->view();
}
public function reset_complete()
{
$this->set('message', t('Languages reset.'));
$this->view();
}
public function reset_languages()
{
if (Core::make('token')->validate('reset_languages')) {
$u = new \User();
$extractor = Core::make('multilingual/extractor');
if ($u->isSuperUser()) {
$list = Section::getList();
foreach ($list as $section) {
// now we load the translations that currently exist for each section
$extractor->deleteSectionTranslationFile($section);
}
$extractor->clearTranslationsFromDatabase();
$this->redirect('/dashboard/system/multilingual/translate_interface', 'reset_complete');
}
} else {
$this->error->add(t('Only the admin user may reset all strings.'));
}
}
public function submit()
{
$extractor = Core::make('multilingual/extractor');
if ($this->post('action') == 'reload') {
if (Core::make('token')->validate()) {
// First, we look in all the site sources for PHP code with GetText
$baseTranslations = $extractor->extractTranslatableSiteStrings();
// $translations contains all of our site translations.
$list = Section::getList($this->getSite());
$defaultSourceLocale = $this->getSite()
->getConfigRepository()->get('multilingual.default_source_locale');
foreach ($list as $section) {
/* @var $section \Concrete\Core\Multilingual\Page\Section\Section */
if ($section->getLocale() != $defaultSourceLocale) {
$localeTranslations = clone $baseTranslations;
/* @var $localeTranslations \Gettext\Translations */
$localeTranslations->setLanguage($section->getLocale());
$localeTranslations->setPluralForms($section->getNumberOfPluralForms(), $section->getPluralsRule());
// now we load the translations that currently exist for each section
$extractor->mergeTranslationsWithSectionFile($section, $localeTranslations);
$extractor->mergeTranslationsWithCore($section, $localeTranslations);
$extractor->mergeTranslationsWithPackages($section, $localeTranslations);
$extractor->saveSectionTranslationsToFile($section, $localeTranslations);
// now that we've updated the translation file, we take all the translations and
// we insert them into the database so we can update our counts, and give the users
// a web interface
$extractor->saveSectionTranslationsToDatabase($section, $localeTranslations);
}
}
$this->redirect('/dashboard/system/multilingual/translate_interface', 'reloaded');
} else {
$this->error->add(Core::make('token')->getErrorMessage());
}
}
if ($this->post('action') == 'export') {
if (Core::make('token')->validate()) {
$defaultSourceLocale = $this->getSite()->getConfigRepository()->get('multilingual.default_source_locale');
$list = Section::getList();
foreach ($list as $section) {
if ($section->getLocale() != $defaultSourceLocale) {
$translations = $section->getSectionInterfaceTranslations();
$extractor->mergeTranslationsWithSectionFile($section, $translations);
$extractor->saveSectionTranslationsToFile($section, $translations);
}
}
\Localization::clearCache();
$this->redirect('/dashboard/system/multilingual/translate_interface', 'exported');
} else {
$this->error->add(Core::make('token')->getErrorMessage());
}
}
$this->view();
}
public function save_translation()
{
$result = new EditResponse();
try {
$token = $this->app->make('token');
if (!$token->validate('translate/save')) {
throw new \Exception(t('Invalid token, please refresh and try again.'));
}
$translation = null;
$mtID = @intval($this->post('id'));
if ($mtID > 0) {
$translation = Translation::getByRecordID($mtID);
}
if (!isset($translation)) {
throw new \Exception(t('Invalid parameter received: %s', 'id'));
}
$singular = '';
$plurals = array();
if ($this->post('clear') !== '1') {
$translated = $this->post('translated');
if ((!is_array($translated)) || (count($translated) < 1)) {
throw new \Exception(t('Invalid parameter received: %s', 'translated'));
}
if ($translation->hasPlural()) {
$singular = array_shift($translated);
$plurals = $translated;
} else {
if (count($translated) !== 1) {
throw new \Exception(t('Invalid parameter received: %s', 'translated'));
}
$singular = $translated[0];
}
}
$translation->updateTranslation($singular, $plurals);
} catch (\Exception $x) {
$result->setError($x);
}
$result->outputJSON();
}
public function export_translations($localeCode)
{
$result = new EditResponse();
try {
if (!Core::make('token')->validate('export_translations')) {
throw new \Exception(Core::make('token')->getErrorMessage());
}
$section = Section::getByLocale($localeCode);
if (is_object($section) && (!$section->isError())) {
if ($section->getLocale() == $this->site->getConfigRepository()->get('multilingual.default_source_locale')) {
$section = null;
}
} else {
$section = null;
}
if (!isset($section)) {
throw new \Exception(t('Invalid language identifier'));
}
$translations = $section->getSectionInterfaceTranslations();
$extractor = Core::make('multilingual/extractor');
$extractor->mergeTranslationsWithSectionFile($section, $translations);
$extractor->saveSectionTranslationsToFile($section, $translations);
\Localization::clearCache();
$result->setAdditionalDataAttribute('newToken', Core::make('token')->generate('export_translations'));
$result->message = t('The translations have been exported to file and will be used by the website.');
} catch (\Exception $x) {
$result->setError($x);
}
$result->outputJSON();
}
public function translate_po($mtSectionID = false)
{
$this->view();
$mtSectionID = intval($mtSectionID);
$section = Section::getByID(intval($mtSectionID));
if ($section) {
$translations = $section->getSectionInterfaceTranslations();
$this->requireAsset('core/translator');
$this->set('section', $section);
$jsonTranslations = array();
$numPlurals = $section->getNumberOfPluralForms();
foreach ($translations as $translation) {
/* @var $translation \Concrete\Core\Multilingual\Page\Section\Translation */
$jsonTranslation = array();
$jsonTranslation['id'] = $translation->getRecordID();
if ($translation->hasContext()) {
$jsonTranslation['context'] = $translation->getContext();
}
$jsonTranslation['original'] = $translation->getOriginal();
if ($translation->hasPlural()) {
$jsonTranslation['originalPlural'] = $translation->getPlural();
}
if ($translation->hasTranslation()) {
$t = array($translation->getTranslation());
if (($numPlurals > 1) && $translation->hasPlural()) {
$t = array_merge($t, $translation->getPluralTranslation());
}
$jsonTranslation['translations'] = $t;
}
$extractedComments = '';
if ($translation->hasExtractedComments()) {
$extractedComments = trim(implode("\n", $translation->getExtractedComments()));
}
if ($extractedComments !== '') {
$jsonTranslation['comments'] = $extractedComments;
}
if ($translation->hasReferences()) {
$jsonTranslation['references'] = $translation->getReferences();
}
$jsonTranslations[] = $jsonTranslation;
}
$this->set('translations', $jsonTranslations);
} else {
$this->redirect('/dashboard/system/multilingual/translate_interface');
}
}
}