From 5fb52e9d1c2a216c89d79d94b06bd6ba243a9301 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 19 Jan 2023 10:47:09 -0500 Subject: [PATCH] Add suggestion from PR #237 to support custom rows definition for translatable fields and have it use that when the user is editing the translation text. To use, specify "rows=3" somewhere in the translation comment, i.e. `__('text'); // rows=3`. Also added support for `type=name` in the comment, that lets you specify what Inputfield type to use. To use, replace the `name` part with "text", "textarea", "email", etc. Or if you prefer you can use the full Inputfield name, i.e. InputfieldText, InputfieldTextarea, etc. Co-authored-by: pine3ree --- .../ProcessLanguageTranslator.module | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/wire/modules/LanguageSupport/ProcessLanguageTranslator.module b/wire/modules/LanguageSupport/ProcessLanguageTranslator.module index 82ddc4a6..d8eabcf9 100644 --- a/wire/modules/LanguageSupport/ProcessLanguageTranslator.module +++ b/wire/modules/LanguageSupport/ProcessLanguageTranslator.module @@ -5,7 +5,7 @@ * * This is the process assigned to the processwire/setup/language-translator/ page. * - * ProcessWire 3.x, Copyright 2022 by Ryan Cramer + * ProcessWire 3.x, Copyright 2023 by Ryan Cramer * https://processwire.com * * @@ -24,7 +24,7 @@ class ProcessLanguageTranslator extends Process { return array( 'title' => __('Language Translator', __FILE__), 'summary' => __('Provides language translation capabilities for ProcessWire core and modules.', __FILE__), - 'version' => 102, + 'version' => 103, 'author' => 'Ryan Cramer', 'requires' => 'LanguageSupport', 'permission' => 'lang-edit' @@ -396,20 +396,40 @@ class ProcessLanguageTranslator extends Process { protected function executeEditField($hash, $untranslated, $translated, $alternates) { /** @var InputfieldText|InputfieldTextarea $field */ - - if(strpos($untranslated, "\n") !== false) { - $qty = substr_count($untranslated, "\n")+1; - $qty2 = substr_count($translated, "\n")+1; - if($qty2 > $qty) $qty = $qty2; - $field = $this->wire()->modules->get("InputfieldTextarea"); - $field->attr('rows', $qty > 2 ? $qty : 2); - } else if(strlen($untranslated) < 128) { - $field = $this->wire()->modules->get("InputfieldText"); - } else { - $field = $this->wire()->modules->get("InputfieldTextarea"); - $field->attr('rows', 3); + + $comment = isset($this->comments[$hash]) ? $this->comments[$hash] : ''; + $type = ''; + + if(stripos($comment, 'type=') !== false && preg_match('!type=(\w+)!i', $comment, $matches)) { + // if type=value appears in comment then use value as Inputfield module name + $type = ucfirst($matches[1]); + $comment = str_replace($matches[0], '', $comment); } - + + if(stripos($comment, 'rows=') !== false && preg_match('!rows=(\d+)!i', $comment, $matches)) { + // if rows=3 appears in comment then accept it as rows attribute for input + $rows = (int) $matches[1]; + $comment = str_replace($matches[0], '', $comment); + } else if(strpos($untranslated, "\n") !== false) { + $qty1 = substr_count($untranslated, "\n")+1; + $qty2 = substr_count($translated, "\n")+1; + $rows = max(2, $qty1, $qty2); + } else if(strlen($untranslated) < 128) { + $rows = 1; + } else { + $rows = 3; + } + + if(empty($type)) { + $type = $rows > 1 ? 'InputfieldTextarea' : 'InputfieldText'; + } else if(strpos($type, 'Inputfield') !== 0) { + $type = "Inputfield$type"; + } + + $field = $this->wire()->modules->get($type); + if(!$field) $field = $this->wire()->modules->get('InputfieldText'); + if($rows > 1 && $field instanceof InputfieldTextarea) $field->attr('rows', $rows); + /** @var InputfieldText $field */ $field->attr('id+name', $hash); $field->set('textFormat', Inputfield::textFormatNone); @@ -418,8 +438,6 @@ class ProcessLanguageTranslator extends Process { $field->addClass(strlen($translated) ? 'translated' : 'untranslated', 'wrapClass'); $field->addClass('translatable'); - $comment = isset($this->comments[$hash]) ? $this->comments[$hash] : ''; - if($comment) { if(preg_match('{^(.*?)//(.*)$}', $comment, $m)) { if(trim($m[1]) != trim($untranslated)) {