Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion static-translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use \Kirby\Panel\Topbar;
use \l;
use \r;
use header;
use upload;

if(class_exists('Panel')) {

Expand Down Expand Up @@ -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', "<?php \n\n" . join("\n", $codelines));
}

return go(panel()->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 {
Expand Down Expand Up @@ -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')
];
}
12 changes: 12 additions & 0 deletions views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
<input class="btn btn-rounded btn-submit" type="submit" value="<?php echo l::get('save'); ?>">
</fieldset>
</form>

<h2 class="hgroup hgroup-single-line hgroup-compressed cf"><span class="hgroup-title">Import / Export</span></h2>
<div class="section">
<a href="<?php echo panel()->urls()->index . '/translations/csv' ?>" class="btn btn-rounded">Download as CSV</a>
</div>
<div class="section">
<form method="POST" enctype="multipart/form-data" action="<?php echo panel()->urls()->index . '/translations/csv' ?>" class="form-upload-translations">
<input class="btn btn-rounded btn-submit" type="submit" value="Upload">
<input type="file" name="file" accept="text/csv">
<input type="hidden" name="csrf" value="<?php echo panel()->csrf() ?>">
</form>
</div>
</div>
</div>

Expand Down