-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatic-translations.php
183 lines (143 loc) · 4.56 KB
/
static-translations.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
namespace Toastlab\Kirby\Plugins\Translations;
use \Kirby\Panel\Topbar;
use \l;
use \r;
use \c;
use \s;
use \yaml;
use \Exception;
if(class_exists('Panel')) {
class TranslationsController extends \Kirby\Panel\Controllers\Base {
public static function setToCode($code, $language) {
$languagesRoot = panel()->site()->kirby()->roots()->languages();
$file = $languagesRoot . DS . $code;
$matches = glob("$file.*");
if(count($matches) == 0) {
//if there is no file, create it as php
self::setToPHP("$file.php", $language);
} else {
$file = $matches[0];
$info = pathinfo($file);
switch (strtolower($info['extension'])) {
case 'yml':
case 'yaml':
self::setToYML($file, $language);
break;
case 'php':
self::setToPHP($file, $language);
break;
}
}
}
public static function getFromCode($code) {
$languagesRoot = panel()->site()->kirby()->roots()->languages();
$file = $languagesRoot . DS . $code;
if(file_exists("$file.php")) {
return self::getFromPHP("$file.php");
} elseif(file_exists("$file.yml")) {
return self::getFromYML("$file.yml");
} elseif(file_exists("$file.yaml")) {
return self::getFromYML("$file.yaml");
}
return [];
}
public static function getFromPHP($file) {
$backup = l::$data;
l::$data = array();
ob_start();
include $file;
ob_end_clean();
$data = l::$data;
l::$data = $backup;
return $data;
}
public static function setToPHP($file, $language) {
$content = "<?php \n\n";
foreach ($language as $key => $value) {
$content .= 'l::set(\'' . addcslashes($key, '\\\'') . '\', \'' . addcslashes($value, '\\\'') . '\');';
$content .= "\n";
}
file_put_contents($file, $content);
}
public function setToYML($file, $language) {
yaml::write($file, $language);
}
public static function getFromYML($file) {
return yaml::read($file);
}
public function view($file, $data = array()) {
return new TranslationsView($file, $data);
}
public function getTranslations() {
$translations = array();
$languages = [];
foreach (c::get('languages') as $lang) {
$code = $lang['code'];
if(isset($lang['default']) && $lang['default']) {
//the default language should be first
array_unshift($languages, $code);
} else {
$languages[] = $code;
}
$translations[$code] = self::getFromCode($code);
}
//merge keys from all languages
$keys = [];
foreach ($translations as $code => $data) {
foreach ($data as $key => $value) {
$keys[$key] = 1;
}
}
$keys = array_keys($keys);
return compact('languages', 'translations', 'keys');
}
public function setTranslations() {
$post = r::data("jsondata");
$post = json_decode($post, true);
foreach (c::get('languages') as $lang) {
$code = $lang['code'];
if($post[$code]) {
self::setToCode($code, $post[$code]);
}
}
panel()->kirby()->cache()->flush();
}
public function index() {
if(r::is('post')) {
$csrf = get('csrf');
if(empty($csrf) or $csrf !== s::get('kirby_panel_csrf')) {
try {
panel()->user()->logout();
} catch(Exception $e) {}
panel()->redirect('login');
} else {
$this->setTranslations();
return panel()->notify(':)');
}
}
return $this->screen('index', new TopbarGenerator(), $this->getTranslations());
}
}
class TopbarGenerator {
public function topbar(Topbar $topbar) {
$topbar->append(panel()->site()->url() . '/panel/translations', 'Translations');
}
}
class TranslationsView extends \Kirby\Panel\View {
public function __construct($file, $data = array()) {
parent::__construct($file, $data);
$this->_root = __DIR__ . DS . 'views';
}
}
$panel = panel();
$panel->routes[] = [
'pattern' => 'translations',
'action' => function() {
$ctrl = new TranslationsController();
return $ctrl->index();
},
'method' => 'GET|POST',
'filter' => array('auth')
];
}