diff --git a/static-translations.php b/static-translations.php index 1526682..5df909f 100644 --- a/static-translations.php +++ b/static-translations.php @@ -4,6 +4,8 @@ use \Kirby\Panel\Topbar; use \l; use \r; +use header; +use upload; if(class_exists('Panel')) { @@ -90,6 +92,57 @@ public function index() { } return $this->screen('index', new TopbarGenerator(), $this->getTranslations()); } + + public function csv() { + if(r::is('post')) { + panel()->csrfCheck(); + + $rawdata = file_get_contents($_FILES['file']['tmp_name']); + $csv = array_map('str_getcsv', explode("\n", $rawdata)); + + $columns = array_shift($csv); + $translations = array(); + + foreach ($csv as $row) { + $key = $row[0]; + if ($key == "") continue; + + for ($i = 1; $i < count($columns); $i++) { + $lang = $columns[$i]; + $value = $row[$i]; + $translations[$lang][] = 'l::set(\'' . addcslashes($key, '\\\'') . '\', \'' . addcslashes($value, '\\\'') . '\');'; + } + } + + $languagesRoot = panel()->site()->kirby()->roots()->languages() . DS; + foreach ($translations as $lang => $codelines) { + file_put_contents($languagesRoot . $lang . '.php', "urls()->index . '/translations'); + } + + $data = $this->getTranslations(); + $csv = '"key"'; + foreach ($data['languages'] as $lang) { + $csv = $csv . ',"' . $lang . '"'; + } + $csv .= "\n"; + foreach ($data['translations'] as $key => $strings) { + $csv .= '"' . $key . '"'; + foreach ($data['languages'] as $lang) { + $csv .= ',"' . str_replace("\"", "\"\"", $strings[$lang]) . '"'; + } + $csv .= "\n"; + } + + header::download(array( + 'name' => "translations.csv", + 'size' => strlen($csv), + 'mime' => "text/csv" + )); + die($csv); + } } class TopbarGenerator { @@ -117,5 +170,13 @@ public function __construct($file, $data = array()) { 'method' => 'GET|POST', 'filter' => array('auth') ]; - + $panel->routes[] = [ + 'pattern' => 'translations/csv', + 'action' => function() { + $ctrl = new TranslationsController(); + return $ctrl->csv(); + }, + 'method' => 'GET|POST', + 'filter' => array('auth') + ]; } diff --git a/views/index.php b/views/index.php index 51872a7..6c21e06 100644 --- a/views/index.php +++ b/views/index.php @@ -30,6 +30,18 @@ + +