1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-18 20:41:16 +02:00

Some updates for processwire/processwire-issues#126 plus some improvements to ProcessLanguage and add a refresh link in the language translation phrase live-search

This commit is contained in:
Ryan Cramer
2019-02-22 07:43:47 -05:00
parent f968137514
commit 41e2bbd22b
2 changed files with 47 additions and 17 deletions

View File

@@ -6,7 +6,7 @@
* It also contains the hooks for altering the output of the InputfieldFile to hold language info and links. * It also contains the hooks for altering the output of the InputfieldFile to hold language info and links.
* This is the process assigned to processwire/setup/languages/. * This is the process assigned to processwire/setup/languages/.
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2019 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* *
@@ -102,19 +102,24 @@ class ProcessLanguage extends ProcessPageType {
} }
public function processInputfieldFileInput(HookEvent $event) { public function processInputfieldFileInput(HookEvent $event) {
$event->object->overwrite = true; /** @var InputfieldFile $inputfield */
$inputfield = $event->object;
$inputfield->overwrite = true;
} }
/** /**
* Hook for before InputfieldFile::render * Hook for before InputfieldFile::render
* *
* In this case we add an 'edit' link to the translator and some info about the translation file. * In this case we add an 'edit' link to the translator and some info about the translation file.
*
* @param HookEvent $event
* *
*/ */
public function renderInputfieldFile(HookEvent $event) { public function renderInputfieldFile(HookEvent $event) {
$language = $this->wire('process')->getPage(); /** @var InputfieldFile $inputfield */
$inputfield = $event->object; $inputfield = $event->object;
$language = $this->wire('process')->getPage();
/** @var Pagefiles $pagefiles */ /** @var Pagefiles $pagefiles */
$pagefiles = $inputfield->attr('value'); $pagefiles = $inputfield->attr('value');
@@ -134,7 +139,8 @@ class ProcessLanguage extends ProcessPageType {
} }
public function renderInputfieldForm(HookEvent $event) { public function renderInputfieldForm(HookEvent $event) {
/** @var InputfieldForm $form */
$form = $event->object; $form = $event->object;
$language = $this->getPage(); $language = $this->getPage();
if(!$language->id) return; if(!$language->id) return;
@@ -143,15 +149,17 @@ class ProcessLanguage extends ProcessPageType {
$inputfield->label = $this->_('Live Search'); $inputfield->label = $this->_('Live Search');
$inputfield->icon = 'search'; $inputfield->icon = 'search';
$placeholder = $this->_('Text to search for'); $placeholder = $this->_('Text to search for');
$refreshUrl = "../../language-translator/add/?language_id=$language->id&refresh=1";
$refreshLabel = $this->_('Refresh search phrase index');
if(!is_file($file)) { if(!is_file($file)) {
$inputfield->value = "<p><a href='../../language-translator/add/?language_id=$language->id&refresh=1'>" . $inputfield->value = "<p><a href='$refreshUrl'>" .
$this->_('Click here to build search phrase index') . "</a></p>"; $this->_('Click here to build search phrase index') . "</a></p>";
} else { } else {
$phrases = file_get_contents($file); $phrases = file_get_contents($file);
$phrases = str_replace(array('"', "\n"), array(' ', ' '), $phrases); $phrases = str_replace(array('"', "\n", "<", ">"), ' ', $phrases);
$inputfield->value = $inputfield->value =
"<script>" . "<script>" .
@@ -163,8 +171,9 @@ class ProcessLanguage extends ProcessPageType {
$this->_('Click found matches to edit translation or add file (if not already present).') . $this->_('Click found matches to edit translation or add file (if not already present).') .
"<p>" . "<p>" .
"<p>" . "<p>" .
"<input type='text' class='language-phrase-search' style='width:50%' name='_q' placeholder='$placeholder' /> " . "<input type='text' class='language-phrase-search InputfieldIgnoreChanges' style='width:50%' name='_q' placeholder='$placeholder' /> " .
"<span class='detail language-phrase-search-cnt'></span>" . "<span class='detail language-phrase-search-cnt'></span> &nbsp;" .
"<a class='pw-tooltip' title='$refreshLabel' href='$refreshUrl'>" . wireIconMarkup('refresh') . "</a>" .
"</p>"; "</p>";
} }
@@ -176,6 +185,8 @@ class ProcessLanguage extends ProcessPageType {
* Hook for InputfieldFile::renderItem * Hook for InputfieldFile::renderItem
* *
* In this case we add an 'edit' link to the translator and some info about the translation file. * In this case we add an 'edit' link to the translator and some info about the translation file.
*
* @param HookEvent $event
* *
*/ */
public function renderInputfieldFileItem(HookEvent $event) { public function renderInputfieldFileItem(HookEvent $event) {
@@ -244,15 +255,22 @@ class ProcessLanguage extends ProcessPageType {
* Hook for InputfieldFile::renderUpload * Hook for InputfieldFile::renderUpload
* *
* This just adds a 'new' link to add a new translation file. * This just adds a 'new' link to add a new translation file.
*
* @param HookEvent $event
* *
*/ */
public function renderInputfieldFileUpload(HookEvent $event) { public function renderInputfieldFileUpload(HookEvent $event) {
$translationUrl = $this->translationUrl(); $translationUrl = $this->translationUrl();
$page = $event->arguments[0]->get('page'); /** @var Pagefiles $pagefiles */
$pagefiles = $event->arguments(0);
/** @var Page $page */
$page = $pagefiles->get('page');
/** @var InputfieldFile $inputfield */
$inputfield = $event->object; $inputfield = $event->object;
$out = ''; $out = '';
/** @var InputfieldButton $btn1 */
$btn1 = $this->wire('modules')->get('InputfieldButton'); $btn1 = $this->wire('modules')->get('InputfieldButton');
$btn1->href = "{$translationUrl}add/?language_id={$page->id}"; $btn1->href = "{$translationUrl}add/?language_id={$page->id}";
$btn1->value = $this->_('Find Files to Translate'); $btn1->value = $this->_('Find Files to Translate');
@@ -261,6 +279,7 @@ class ProcessLanguage extends ProcessPageType {
$out .= $btn1->render(); $out .= $btn1->render();
if(count($inputfield->attr('value'))) { if(count($inputfield->attr('value'))) {
/** @var InputfieldButton $btn2 */
$btn2 = $this->wire('modules')->get('InputfieldButton'); $btn2 = $this->wire('modules')->get('InputfieldButton');
$btn2->href = "../download/?language_id={$page->id}&field=" . $inputfield->attr('name'); $btn2->href = "../download/?language_id={$page->id}&field=" . $inputfield->attr('name');
$btn2->value = $this->_('Download ZIP'); $btn2->value = $this->_('Download ZIP');
@@ -282,6 +301,10 @@ class ProcessLanguage extends ProcessPageType {
* Modify the output per-field in the PageType list (template-method) * Modify the output per-field in the PageType list (template-method)
* *
* In this case we make it return a count for the language_files * In this case we make it return a count for the language_files
*
* @param string $name
* @param mixed $value
* @return string
* *
*/ */
protected function renderListFieldValue($name, $value) { protected function renderListFieldValue($name, $value) {
@@ -352,7 +375,7 @@ class ProcessLanguage extends ProcessPageType {
foreach($untranslated as $hash => $text1) { foreach($untranslated as $hash => $text1) {
$text2 = isset($translated[$hash]) ? $translated[$hash]['text'] : ''; $text2 = isset($translated[$hash]) ? $translated[$hash]['text'] : '';
$comment = isset($comments[$hash]) ? $comments[$hash] : ''; $comment = isset($comments[$hash]) ? $comments[$hash] : '';
if(strpos($comment, '//') !== false) list($ignore, $comment) = explode('//', $comment); if(strpos($comment, '//') !== false) list(, $comment) = explode('//', $comment);
$fields = array($text1, $text2, trim($comment), $file, $hash); $fields = array($text1, $text2, trim($comment), $file, $hash);
fputcsv($fp, $fields); fputcsv($fp, $fields);
} }
@@ -405,9 +428,9 @@ class ProcessLanguage extends ProcessPageType {
); );
$n = 0; $n = 0;
$data = array();
$header = array(); $header = array();
$translator = new LanguageTranslator($language); $translator = new LanguageTranslator($language);
$textdomain = '';
$lastTextdomain = ''; $lastTextdomain = '';
$lastFile = ''; $lastFile = '';
$numChanges = 0; $numChanges = 0;
@@ -447,7 +470,7 @@ class ProcessLanguage extends ProcessPageType {
$file = $row['file']; $file = $row['file'];
$hash = $row['hash']; $hash = $row['hash'];
$textOriginal = $row['original']; // $textOriginal = $row['original'];
$textTranslated = $row['translated']; $textTranslated = $row['translated'];
$textdomain = $translator->filenameToTextdomain($file); $textdomain = $translator->filenameToTextdomain($file);
@@ -503,16 +526,18 @@ class ProcessLanguage extends ProcessPageType {
* *
* @param LanguageTranslator $translator * @param LanguageTranslator $translator
* @param string $textdomain * @param string $textdomain
* @param string $filename
* @param int $numChanges * @param int $numChanges
* *
*/ */
protected function processCSV_saveTextdomain(LanguageTranslator $translator, $textdomain, $filename, $numChanges) { protected function processCSV_saveTextdomain(LanguageTranslator $translator, $textdomain, $filename, $numChanges) {
if($filename) { /* ignore, not currently used */ }
$file = $translator->textdomainToFilename($textdomain); $file = $translator->textdomainToFilename($textdomain);
if($numChanges) { if($numChanges) {
try { try {
$translator->saveTextdomain($textdomain); $translator->saveTextdomain($textdomain);
$this->message($this->csvImportLabel . sprintf($this->_('Saved %d change(s) for file: %s'), $numChanges, $file)); $this->message($this->csvImportLabel . sprintf($this->_('Saved %d change(s) for file: %s'), $numChanges, $file));
} catch(Exception $e) { } catch(\Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
} else { } else {

View File

@@ -192,6 +192,11 @@ class ProcessLanguageTranslator extends Process {
'site' => $this->findTranslatableFiles($this->wire('config')->paths->site, $useCache), 'site' => $this->findTranslatableFiles($this->wire('config')->paths->site, $useCache),
'wire' => $this->findTranslatableFiles($this->wire('config')->paths->wire, $useCache) 'wire' => $this->findTranslatableFiles($this->wire('config')->paths->wire, $useCache)
); );
if($this->input->get('refresh')) {
$this->wire('session')->redirect("../../languages/edit/?id={$this->language->id}");
return '';
}
$textdomains = array(); $textdomains = array();
foreach(array('language_files', 'language_files_site') as $fieldName) { foreach(array('language_files', 'language_files_site') as $fieldName) {
@@ -486,7 +491,7 @@ class ProcessLanguageTranslator extends Process {
$this->session->redirect('../add/'); $this->session->redirect('../add/');
} }
$numFound = $this->parseTranslatableFile($file); $this->parseTranslatableFile($file);
/** @var InputfieldForm $form */ /** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm'); $form = $this->modules->get('InputfieldForm');
@@ -658,7 +663,7 @@ class ProcessLanguageTranslator extends Process {
} }
if(is_null($this->fp)) { if(is_null($this->fp)) {
$f = $this->language->filesManager()->path() . '.phrase-index.txt'; // @todo make hidden $f = $this->language->filesManager()->path() . '.phrase-index.txt';
if(is_file($f)) $this->wire('files')->unlink($f); if(is_file($f)) $this->wire('files')->unlink($f);
$this->fp = fopen($f, "a"); $this->fp = fopen($f, "a");
} }
@@ -723,7 +728,7 @@ class ProcessLanguageTranslator extends Process {
$files[$pathname] = $pathname; $files[$pathname] = $pathname;
if(preg_match_all('/\b(?:_[_nx]\(|->_[nx]?\()[\'"](.+?)[\'"]\)/', $text, $matches)) { if(preg_match_all('/\b(?:_[_nx]\(|->_[nx]?\()[\'"](.+?)[\'"]\)/', $text, $matches)) {
foreach($matches[1] as $key => $phrase) { foreach($matches[1] as $key => $phrase) {
$phrase = str_replace(array('|', '^', "\n", "\r", "\t"), ' ', $phrase); $phrase = str_replace(array('|', '^', "\n", "\r", "\t", "&lt;", "&gt;"), ' ', strip_tags($phrase));
fwrite($this->fp, "|$phrase"); fwrite($this->fp, "|$phrase");
} }
$textdomain = $this->translator->filenameToTextdomain($pathname); $textdomain = $this->translator->filenameToTextdomain($pathname);