diff --git a/CHANGELOG.md b/CHANGELOG.md index f81b42a..32288b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ca08d85..9474b0d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/admin/templates/babel_domains.html.twig b/admin/templates/babel_domains.html.twig index 0792bb5..24aeb67 100644 --- a/admin/templates/babel_domains.html.twig +++ b/admin/templates/babel_domains.html.twig @@ -34,15 +34,13 @@ {% if babelstats %} - - {% for domain in domains %} - - {% endfor %} + {% for domain in domains %} + + {% endfor %} - - @@ -110,6 +108,25 @@
- + + + + + + + + + + {% for domain, domainfile in domainfiles %} + + + + + {% endfor %} + +
{{ "PLUGIN_BABEL.DOMAIN"|tu }}{{ "PLUGIN_BABEL.DOMAIN_PACKAGE"|tu }}
{{ domain }}{{ domain ~ '_languages.zip' }}
+ +
{% endblock %} diff --git a/babel.php b/babel.php index 718adee..b207bb7 100644 --- a/babel.php +++ b/babel.php @@ -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'); @@ -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 * diff --git a/blueprints.yaml b/blueprints.yaml index 796580f..08dee98 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Babel -version: 1.0.2 +version: 1.0.3 description: Translation services for GRAV icon: language author: @@ -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: diff --git a/classes/Babel.php b/classes/Babel.php index e4a09fc..31fa3d2 100644 --- a/classes/Babel.php +++ b/classes/Babel.php @@ -11,6 +11,7 @@ use SQLite3; use Grav\Common\File\CompiledYamlFile; use Grav\Plugin\Babel\BabelConnector; +use \Grav\Common\Twig\TwigExtension; class Babel { @@ -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' ], @@ -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 = [ @@ -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; + } + } diff --git a/classes/BabelSearch.php b/classes/BabelSearch.php index 0e43626..360da08 100644 --- a/classes/BabelSearch.php +++ b/classes/BabelSearch.php @@ -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'; diff --git a/languages.yaml b/languages.yaml index 7cf865f..51576d7 100644 --- a/languages.yaml +++ b/languages.yaml @@ -20,4 +20,5 @@ en: RESET: Reset definitions MERGE: Merge EXPORT: Export + DOMAIN_PACKAGE: Exported Language Pack