Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Feb 23, 2017
0 parents commit fc8da9c
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YTraduko
========
62 changes: 62 additions & 0 deletions fragments/ytraduko/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php /** @var rex_fragment $this */ ?>
<form action="<?= $this->context->getUrl() ?>" method="post">
<section class="rex-page-section">
<div class="panel panel-default">
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th style="width: 50px"><?= $this->i18n('ytraduko_key') ?></th>
<th style="width: 400px">de_de</th>
<th><?= $this->escape($this->language) ?></th>
</tr>
</thead>
<tbody>
<?php $file = $this->package->getFile($this->language) ?>
<?php foreach ($this->package->getSource() as $key => $value): ?>
<tr>
<td><code><?= $this->escape($key) ?></code></td>
<td><?= $this->escape($value) ?></td>
<td<?= isset($file[$key]) ? '' : ' class="has-error"' ?>>
<input type="text" class="form-control"
name="ytraduko[<?= $this->escape($this->package->getName()) ?>][<?= $this->escape($key) ?>]"
value="<?= isset($file[$key]) ? $this->escape($file[$key]) : '' ?>"
/>
</td>
</tr>
<?php endforeach ?>
<?php if ($this->package instanceof rex_ytraduko_addon): ?>
<?php foreach ($this->package->getPlugins() as $plugin): ?>
<tr>
<th colspan="3"><?= $this->escape($plugin->getName()) ?></th>
</tr>
<?php $file = $plugin->getFile($this->language) ?>
<?php foreach ($plugin->getSource() as $key => $value): ?>
<tr>
<td><code><?= $this->escape($key) ?></code></td>
<td><?= $this->escape($value) ?></td>
<td<?= isset($file[$key]) ? '' : ' class="has-error"' ?>>
<input type="text" class="form-control"
name="ytraduko[<?= $this->escape($plugin->getName()) ?>][<?= $this->escape($key) ?>]"
value="<?= isset($file[$key]) ? $this->escape($file[$key]) : '' ?>"
/>
</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
<?php endif ?>
</tbody>
</table>
</div>
<div class="panel-footer">
<div class="rex-form-panel-footer">
<div class="btn-toolbar">
<button class="btn btn-save rex-form-aligned pull-right" type="submit"<?= rex::getAccesskey($this->i18n('form_save'), 'save') ?>>
<?= $this->i18n('form_save') ?>
</button>
</div>
</div>
</div>
</div>
</section>
</form>
56 changes: 56 additions & 0 deletions fragments/ytraduko/overview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php /** @var rex_fragment $this */ ?>
<table class="table table-hover">
<thead>
<tr>
<th class="rex-table-icon">&nbsp;</th>
<th style="width: 200px"><?= rex_i18n::msg('package_hname') ?></th>
<th style="width: 100px"><?= rex_i18n::msg('package_hversion') ?></th>
<th style="width: 100px" class="text-center">de_de</th>
<?php foreach ($this->languages as $language): ?>
<th class="text-center"><?= $this->escape($language) ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($this->packages as $package): /** @var rex_ytraduko_package $package */ ?>
<tr>
<td class="rex-table-icon"><i class="rex-icon rex-icon-package-addon"></i></td>
<td><?= $this->escape($package->getName()) ?></td>
<td><?= $this->escape($package->getVersion()) ?></td>
<td class="text-center"><?= $package->countKeys() ?></td>
<?php foreach ($this->languages as $language): ?>
<?php $percentage = (int) (100 * $package->countLanguageKeys($language) / $package->countKeys()); ?>
<td class="text-center">
<a href="<?= $this->context->getUrl(['language' => $language, 'package' => $package->getName()]) ?>">
<div class="progress" style="margin-bottom: 0">
<div class="progress-bar progress-bar-success" style="width: <?= $percentage ?>%">
<?= $percentage ?> %
</div>
<div class="progress-bar progress-bar-danger" style="width: <?= 100 - $percentage ?>%"></div>
</div>
</a>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
<tfoot>
<tr style="background-color: #dfe3e9">
<td class="rex-table-icon"></td>
<td><b><?= mb_strtoupper($this->i18n('ytraduko_total')) ?></b></td>
<td></td>
<td class="text-center"><b><?= $this->total['de_de'] ?></b></td>
<?php foreach ($this->languages as $language): ?>
<?php $percentage = (int) (100 * $this->total[$language] / $this->total['de_de']); ?>
<td class="text-center">
<div class="progress" style="margin-bottom: 0">
<div class="progress-bar progress-bar-success" style="width: <?= $percentage ?>%">
<?= $percentage ?> %
</div>
<div class="progress-bar progress-bar-danger" style="width: <?= 100 - $percentage ?>%"></div>
</div>
</td>
<?php endforeach ?>
</tr>
</tfoot>
</table>
44 changes: 44 additions & 0 deletions fragments/ytraduko/toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php /** @var rex_fragment $this */ ?>
<ul class="nav navbar-nav">
<li>
<a href="<?= $this->context->getUrl(['language' => null, 'package' => null]) ?>">
<?= $this->i18n('ytraduko_overview') ?>
</a>
</li>
<li class="dropdown">
<?php
$items = array_map(function (rex_ytraduko_package $package) {
return [
'title' => $this->escape($package->getName()),
'href' => $this->context->getUrl(['package' => $package->getName()]),
'active' => $this->package === $package,
];
}, $this->packages);

$this->subfragment('core/dropdowns/dropdown.php', [
'toolbar' => true,
'button_prefix' => $this->i18n('ytraduko_package').': ',
'button_label' => $this->escape($this->package->getName()),
'items' => $items,
]);
?>
</li>
<li class="dropdown">
<?php
$items = array_map(function ($language) {
return [
'title' => $this->escape($language),
'href' => $this->context->getUrl(['language' => $language]),
'active' => $this->language === $language,
];
}, $this->languages);

$this->subfragment('core/dropdowns/dropdown.php', [
'toolbar' => true,
'button_prefix' => $this->i18n('ytraduko_language').': ',
'button_label' => $this->escape($this->language),
'items' => $items,
]);
?>
</li>
</ul>
8 changes: 8 additions & 0 deletions lang/de_de.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ytraduko_overview = Übersicht

ytraduko_package = Paket
ytraduko_language = Sprache

ytraduko_key = Schlüssel

ytraduko_total = Gesamt
58 changes: 58 additions & 0 deletions lib/addon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

class rex_ytraduko_addon extends rex_ytraduko_package
{
/** @var rex_ytraduko_package[] */
private $plugins;

/**
* @param rex_addon $addon
*
* @return null|self
*/
public static function create(rex_addon $addon)
{
$countKeys = 0;
$plugins = [];

foreach ($addon->getRegisteredPlugins() as $plugin) {
if (file_exists($plugin->getPath('lang/de_de.lang'))) {
$package = new rex_ytraduko_package($plugin->getName(), $plugin->getVersion(), $plugin->getPath('lang'));
if ($package->countKeys()) {
$plugins[] = $package;
$countKeys += $package->countKeys();
}
}
}

if ($plugins || file_exists($addon->getPath('lang/de_de.lang'))) {
$addon = new self($addon->getName(), $addon->getVersion(), $addon->getPath('lang'));
$addon->plugins = $plugins;
$addon->countKeys += $countKeys;
if ($addon->countKeys()) {
return $addon;
}
}

return null;
}

/**
* @return rex_ytraduko_package[]
*/
public function getPlugins()
{
return $this->plugins;
}

public function countLanguageKeys($language)
{
$count = parent::countLanguageKeys($language);

foreach ($this->plugins as $plugin) {
$count += $plugin->countLanguageKeys($language);
}

return $count;
}
}
34 changes: 34 additions & 0 deletions lib/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class rex_ytraduko_file extends ArrayObject
{
private $path;

public function __construct($path)
{
$this->path = $path;

if (file_exists($path)) {
$this->load();
}
}

public function getPath()
{
return $this->path;
}

private function load()
{
if (
($content = rex_file::get($this->path)) &&
preg_match_all('/^([^\s]*)\h*=\h*(\V*\S?)\h*$/m', $content, $matches, PREG_SET_ORDER)
) {
foreach ($matches as $match) {
if ($match[2]) {
$this[$match[1]] = $match[2];
}
}
}
}
}
105 changes: 105 additions & 0 deletions lib/package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

class rex_ytraduko_package
{
protected $name;
protected $version;
protected $path;

/** @var rex_ytraduko_file */
protected $source;

protected $countKeys;

/** @var rex_ytraduko_file[] */
protected $files;

protected function __construct($name, $version, $path)
{
$this->name = $name;
$this->version = $version;
$this->path = $path;

$this->source = new rex_ytraduko_file($path.'/de_de.lang');
$this->countKeys = count($this->source);
}

/**
* @return self[]
*/
public static function getAll()
{
$all = [];
$all['core'] = new self('core', rex::getVersion(), rex_path::core('lang'));

$ignore = rex_addon::get('ytraduko')->getProperty('config')['packages_ignore'];

foreach (rex_addon::getRegisteredAddons() as $addon) {
if (in_array($addon->getName(), $ignore)) {
continue;
}

$addon = rex_ytraduko_addon::create($addon);
if ($addon) {
$all[$addon->getName()] = $addon;
}
}

return $all;
}

public function getName()
{
return $this->name;
}

public function getVersion()
{
return $this->version;
}

public function getSource()
{
return $this->source;
}

public function countKeys()
{
return $this->countKeys;
}

public function countLanguageKeys($language)
{
return count($this->getFile($language));
}

/**
* @param string $language
*
* @return rex_ytraduko_file
*/
public function getFile($language)
{
if (!isset($this->files[$language])) {
$this->files[$language] = new rex_ytraduko_file($this->path.'/'.$language.'.lang');
}

return $this->files[$language];
}

public function save($language)
{
$content = rex_file::get($this->source->getPath());
$file = $this->getFile($language);

foreach ($file as $key => $value) {
$content = preg_replace(
'/^('.preg_quote($key, '/').'\h*=\h*)(\V*\S)?\h*$/m',
rtrim('${1}'.$value),
$content
);
}

rex_file::put($file->getPath(), $content);
}
}
16 changes: 16 additions & 0 deletions package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package: ytraduko
version: '0.1-dev'
author: Gregor Harlan
supportpage: www.redaxo.org/de/forum/

page:
title: YTraduko
icon: rex-icon fa-language
perm: ytraduko[]

requires:
redaxo: '^5.3.0'

config:
languages: [en_gb, se_sv]
packages_ignore: [debug, textile]
Loading

0 comments on commit fc8da9c

Please sign in to comment.