Skip to content

Commit

Permalink
Merge pull request #7 from leotiger/develop
Browse files Browse the repository at this point in the history
Make exported language packs available
  • Loading branch information
leotiger authored Aug 8, 2018
2 parents 19d577f + 0a33e33 commit 889c1a9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 79 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v1.0.3
## 08-08-2018

1. [](#new)
* Zip exported domain definitions into language pack zips
* Make domain language packs available in the Babel interface

# v1.0.2
## 08-08-2018

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ handle with Babel in the plugin configuration. The languages handled by Babel do
instance. This allows you to prepare additional languages before activating new ones for your site.

Once you have enabled the Babel languages you can index all language variables and existing translations. There is one magic keyboard shortcut
availabel Command/Ctrl + s which allows you to save right from within of the textarea.
available Command/Ctrl + s which allows you to save right from within of the textarea field used to edit translations.

Babel identifies edited and merged definitions when a re-index takes place. This allows the plugin to only merge edited definitions.

Important: Please merge your changes. Without merging changes are lost once you re-index.

You can export language packs based on domains, e.g. export translations sets related with PLUGIN_ADMIN. The language pack includes all exported
definitions. Please take into account that you have to trigger the export for each language. Export functionality is only available when a language domain
is active but not for all domains and not for the *babelized* filter.

## What's missing

* a common, shared language repository for GRAV
Expand Down
31 changes: 24 additions & 7 deletions admin/templates/babel_domains.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
<!--<input type="text" placeholder="{{ "PLUGIN_BABEL.SEARCH"|t|e }}" />
<br />-->
{% if babelstats %}
<select id="babeldomains">
<select id="babeldomains" data-domainfiles="{{ domainfiles|json_encode() }}">
<option value="*" {{ current_domain == '*' ? 'selected="selected"' : '' }}>{{ "PLUGIN_BABEL.ALL_DOMAINS"|tu }}</option>
<option value="*b" {{ current_domain == '*b' ? 'selected="selected"' : '' }}>{{ "PLUGIN_BABEL.BABEL_TOUCHED"|tu }}</option>
{% for domain in domains %}
<option value="{{ domain }}" {{ current_domain == domain ? 'selected="selected"' : '' }}>{{ domain }}</option>
{% endfor %}
{% for domain in domains %}
<option value="{{ domain }}" {{ current_domain == domain ? 'selected="selected"' : '' }}>{{ domain }}</option>
{% endfor %}
</select>


<table>
<thead>
<tr>
Expand Down Expand Up @@ -110,6 +108,25 @@

</tbody>
</table>


<table id='babel_domain_packages' class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th>{{ "PLUGIN_BABEL.DOMAIN"|tu }}</th>
<th>{{ "PLUGIN_BABEL.DOMAIN_PACKAGE"|tu }}</th>
</tr>
</thead>
<tbody>
{% for domain, domainfile in domainfiles %}
<tr>
<td>{{ domain }}</td>
<td><a href="{{ domainfile }}" class="babel-domainfiles">{{ domain ~ '_languages.zip' }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>

<div

</div>
{% endblock %}
76 changes: 8 additions & 68 deletions babel.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ public function onAdminTwigSiteVariables()

//$this->grav['babel']->createIndex();
} else {

$twig->twig_vars['babel_index_status'] = ['status' => $status, 'msg' => $msg];
// Avoid unncessary processing...
$uri = $this->grav['uri'];
if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) {
return;
}
$this->grav['assets']->addCss('plugin://babel/bower_components/bootstrap/dist/css/bootstrap.css');
$this->grav['assets']->addCss('plugin://babel/assets/admin/babel.css');
$this->grav['assets']->addCss('plugin://babel/bower_components/datatables.net-bs/css/dataTables.bootstrap.css');
Expand All @@ -107,81 +111,17 @@ public function onAdminTwigSiteVariables()
$this->grav['assets']->addJs('plugin://babel/bower_components/datatables.net/js/jquery.dataTables.js');
$this->grav['assets']->addJs('plugin://babel/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js');
$this->grav['assets']->addJs('plugin://babel/bower_components/datatables.net-responsive/js/dataTables.responsive.min.js');
$uri = $this->grav['uri'];
if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) {
//return;
}
$domain = $uri->param('domain');
$twig->twig_vars['current_domain'] = $domain;
$twig->twig_vars['babelstats'] = $this->babel->getBabelStats($domain);

$twig->twig_vars['domains'] = $this->babel->getBabelDomains();
/*
$translations = Grav::instance()['languages'];
$domains = $this->babel->getBabelDomains();
$twig->twig_vars['domains'] = $domains;
$twig->twig_vars['domainfiles'] = $this->babel->getDomainFiles($domains);

$codes = Grav::instance()['config']->get('plugins.babel.translation_sets', ['en']);
$babeldefinitions = [];
foreach($codes as $code => $langdef) {
$babels = $translations->get($langdef);
if (is_array($babels) && count($babels)) {
$this->runBabelDefs($babeldefinitions, $babels, $langdef, '');
}
}
*
*/
}
}
/*
private function runBabelDefs(&$babeldefinitions, $babels, $code, $route, $level = 0) {
foreach($babels as $key => $babel) {
if (!is_array($babel) && !in_array($key, $babeldefinitions)) {
$id = $route . '.' . $key;
$babeldefinitions[$id] = $id;
//Grav::instance()['log']->info($id);
} elseif (is_array($babel)) {
if ($level == 0) {
$route = $key;
} else {
$route = $route . '.' . $key;
}
$this->runBabelDefs($babeldefinitions, $babel, $code, $route, $level + 1);
}
}
}

private function createBabelDef($definition, $code) {
$babelobj = new \stdClass();
$id = $code . '.' . $definition;
$babelobj->route = $id;
$babelobj->domain = explode('.', $definition)[0];
$babelobj->language = $code;
$translation = $this->grav['language']->translate($definition, [$code]);
$babelobj->definition = $translation;// . ' ' . $id . ' ' . str_replace('.', ' ', $id);
return $babelobj;
//$babelobjects[] = $babelobj;
}

private function runBabel(&$babelobjects, $babels, $code, $route) {
foreach($babels as $key => $babel) {
if (!is_array($babel) && count(explode('.', $route)) > 1) {
$babelobj = new \stdClass();
$id = $route . '.' . $key;
$babelobj->route = $id;
$babelobj->domain = explode('.', $route)[1];
$babelobj->language = explode('.', $route)[0];
$babelobj->definition = $babel . ' ' . $id . ' ' . str_replace('.', ' ', $id);
$babelobjects[] = $babelobj;
} elseif (is_array($babel)) {
$this->runBabel($babelobjects, $babel, $code, $route . '.' . $key);
}
}
}
*/

/**
* Wrapper to get the number of documents currently indexed
*
Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Babel
version: 1.0.2
version: 1.0.3
description: Translation services for GRAV
icon: language
author:
Expand All @@ -13,7 +13,7 @@ docs: https://github.com/leotiger/grav-plugin-babel/blob/master/README.md
license: MIT

dependencies:
- { name: grav, version: '>=1.1.9' }
- { name: grav, version: '>=1.4.0' }
- { name: admin, version: '>=1.8.0' }

form:
Expand Down
18 changes: 17 additions & 1 deletion classes/Babel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SQLite3;
use Grav\Common\File\CompiledYamlFile;
use Grav\Plugin\Babel\BabelConnector;
use \Grav\Common\Twig\TwigExtension;

class Babel
{
Expand All @@ -19,6 +20,7 @@ class Babel
protected $bool_characters = ['-', '(', ')', 'or'];
protected $index = 'babel.index';
protected $babelizations = [];
protected $export_path = 'user://data/babel';

public static $codes = [
'af' => [ 'name' => 'Afrikaans', 'nativeName' => 'Afrikaans' ],
Expand Down Expand Up @@ -178,11 +180,11 @@ public function __construct($options = [])
$snippet = Grav::instance()['config']->get('plugins.babel.snippet', 300);
$data_path = Grav::instance()['locator']->findResource('user://data', true) . '/babel';


if (!file_exists($data_path)) {
mkdir($data_path);
}


$dbloc = $data_path . DS . $this->index;

$defaults = [
Expand Down Expand Up @@ -465,4 +467,18 @@ public function mergeBabel()
{
return $this->babel->mergeBabel();
}

public function getDomainFiles($domains) {
$domainfiles = [];
$twig_extension = new TwigExtension();
$zip_path = Grav::instance()['locator']->findResource($this->export_path, true) . '/zips';
foreach($domains as $domain) {
$zipFile = $zip_path . DS . $domain . '_languages.zip';
if (file_exists($zipFile)) {
$domainfiles[$domain] = $twig_extension->urlFunc($this->export_path . '/zips/' . $domain . '_languages.zip', true);
}
}
return $domainfiles;
}

}
23 changes: 23 additions & 0 deletions classes/BabelSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,31 @@ public function exportBabel($post) {
$yaml->file($yamlfile);
$yaml->save();
}
$this->zipExport($this->config['storage'], $pathToExport, $domain);
}

private function zipExport($storage, $folder, $domain) {
$zipDir = $storage . 'zips';
if (!file_exists($zipDir)) {
mkdir($zipDir);
}
$zipFile = $zipDir . DS . $domain . '_languages.zip';
$zipArchive = new \ZipArchive();

if (!$zipArchive->open($zipFile, \ZipArchive::CREATE)) {
Grav::instance()['log']->info('Failed to create zip archive for domain ' . $domain . ' export.');
return;
}
$globOptions = array('remove_all_path' => TRUE);
$zipArchive->addGlob($storage . $domain . DS . '*' . YAML_EXT, GLOB_BRACE, $globOptions);
if (!$zipArchive->status == \ZipArchive::ER_OK) {
Grav::instance()['log']->info('addGlob failed...');
return;
}
$zipArchive->close();
}


public function mergeBabel() {
if (is_null($this->index)) {
$pathToIndex = $this->config['storage'] . 'babel.index';
Expand Down
1 change: 1 addition & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ en:
RESET: Reset definitions
MERGE: Merge
EXPORT: Export
DOMAIN_PACKAGE: Exported Language Pack

0 comments on commit 889c1a9

Please sign in to comment.