This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationManagement.php
222 lines (189 loc) · 7.76 KB
/
TranslationManagement.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
<?php
/**
* Yii extension for manage translation from CPhpMessageSource
*
* @author Volodymyr Moskalyk <[email protected]>
* @version 1.0.0
* @license Apache License Version 2.0, January 2004, http://www.apache.org/licenses/
*/
class TranslationManagement extends CInputWidget {
/**
* Default message language
* @var
*/
public $default_language;
/**
* Language array of translated language
* @var array
*/
public $translated_languages = array();
/**
* Here we start
*/
public function run() {
Yii::setPathOfAlias('TranslationManagement', dirname(__FILE__));
$this->registerAssets();
if (empty($this->default_language))
$this->default_language = Yii::app()->params['defaultLanguage'];
if (!$this->translated_languages)
$this->translated_languages = Yii::app()->params['translatedLanguages'];
//if (Yii::app()->user->isGuest) throw new CHttpException(404);
//if (!Yii::app()->user->checkAccess('superadmin') and !Yii::app()->user->checkAccess('translator'))
//throw new CHttpException(404);
if (Yii::app()->getRequest()->isAjaxRequest) {
foreach ($_POST as $key => $value) {
if (in_array($key, ['key_name', 'file_name'])) continue;
$tmp = explode('_', $key);
$this->translateOneMessage(urldecode($_POST['key_name']), $value, $tmp[1], $_POST['file_name']);
}
ob_clean();
$data = $this->getMessage(Yii::app()->basePath.'/messages/'.$this->default_language.'/', $_POST['file_name']);
echo Yii::app()->controller->renderPartial('TranslationManagement.views.translation_one', [
'category' => ['id' => str_replace('.php', '', $_POST['file_name']), 'content' => $data['rows'], 'active' => true],
'translated_languages' => $this->translated_languages
]);
Yii::app()->end();
}
$this->render('translation', ['categories' => $this->getCategories(), 'translated_languages' => $this->translated_languages]);
}
/**
* Register bootstrap and js, css
*/
public function registerAssets() {
Yii::app()->getClientScript()->scriptMap['bootstrap.js'] = false; // if bootstrap.js already register remove it. We use bootstrap.min.js
Yii::app()->getClientScript()
->addPackage('TranslationManagement', array(
'baseUrl' => CHtml::asset(Yii::getPathOfAlias('TranslationManagement.assets')),
'js' => ['js/bootstrap.min.js', 'js/translation.js'],
'css' => ['css/bootstrap.min.css', 'css/translation.css'],
'depends' => ['jquery'],
))
->registerPackage('TranslationManagement');
}
/**
* Get array of all message for all category
*
* @param bool $directory
* @return array
*/
public function getCategories($directory = false) {
$categories = [];
$dir = $directory ? $directory : Yii::app()->basePath.'/messages/'.$this->default_language.'/';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file == '.' || $file == '..') continue;
$data = $this->getMessage($dir, $file);
$name = str_replace('.php', '', $file);
$categories[$name] = array(
'label' => $name.' <span class="badge badge-'.$data['total_class'].'">'.$data['total_count'].'%</span>',
'id' => $name,
'content' => $data['rows'],
);
}
closedir($dh);
}
}
ksort($categories);
$categories[key($categories)]['active'] = true; // make first element active
return $categories;
}
/**
* Generate table with message for one category
*
* @param $directory
* @param $file
* @return array
* @throws Exception
*/
public function getMessage($directory, $file) {
$message = $row = [];
$count_bad = 0;
foreach ($this->translated_languages as $key => $lang) {
$path = str_replace('/'.$this->default_language.'/', '/'.$key.'/', $directory);
if (file_exists($path.$file)) {
$message[$key] = require($path.$file);
} else {
throw new Exception(
sprintf('File "%s" with translation does not exist. Please run "/protected$ ./yiic message messages/config.php" to synchronise you translation files', $path.$file)
);
}
}
$count_all = count($message[$this->default_language]) * count(array_keys($this->translated_languages));
foreach ($message[$this->default_language] as $key_t => $value_t) {
$row[$key_t] = '<tr>';
$default_message = ($value_t) ? $value_t : $key_t;
$row[$key_t] .= '<td>'.$default_message.'</td>';
foreach (array_keys($this->translated_languages) as $language) {
if ($language == $this->default_language) continue;
if (isset($message[$language][$key_t]) and $message[$language][$key_t]) {
$row[$key_t] .= '<td>'.$message[$language][$key_t].'</td>';
} else {
$row[$key_t] .= '<td class="error"></td>';
$count_bad++;
}
}
$row[$key_t] .= '<td>'.
CHtml::link('<i class="icon-pencil"></i>', '#', [
'class' => 'update',
'data-file' => $file,
'data-key' => urlencode($key_t),
'data-toggle' => 'modal',
'data-target' => '#translate_modal'
]).'</td>';
$row[$key_t] .= '</tr>';
}
$result = 100 - ($count_bad * 100 / $count_all);
if ($result == 100) {
$class = 'success';
} elseif ($result >= 50 and $result <= 99.99) {
$class = 'warning';
} else {
$class = 'important';
}
return array('rows' => $row, 'total_class' => $class, 'total_count' => number_format($result, 2));
}
/**
* Save translated message to file
*
* @see \MessageCommand::generateMessageFile
* @param $key
* @param $message
* @param $lang
* @param $file
* @throws Exception
*/
public static function translateOneMessage($key, $message, $lang, $file) {
$file = Yii::app()->basePath.'/messages/'.$lang.'/'.$file;
$translated = require($file);
$translated[$key] = $message;
//ksort($translated);
$array = str_replace("\r", '', var_export($translated, true));
$content = <<<EOD
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yiic message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE, this file must be saved in UTF-8 encoding.
*/
return $array;
EOD;
if (is_writable($file)) {
file_put_contents($file, $content);
} else {
throw new Exception(sprintf('File "%s" is not writable', $file));
}
}
}