From a71623217208e006a30748eb2a29f4ffdd450612 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 16 Feb 2023 09:21:53 -0500 Subject: [PATCH] Add feature request processwire/processwire-requests#480 to support other file extensions for translatable files in ProcessLanguageTranslator.module as a module config setting --- .../LanguageSupport/LanguageTranslator.php | 1 + .../ProcessLanguageTranslator.module | 61 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/wire/modules/LanguageSupport/LanguageTranslator.php b/wire/modules/LanguageSupport/LanguageTranslator.php index 3ae57846..49eac2d7 100644 --- a/wire/modules/LanguageSupport/LanguageTranslator.php +++ b/wire/modules/LanguageSupport/LanguageTranslator.php @@ -669,6 +669,7 @@ class LanguageTranslator extends Wire { case 'system': $v = $this->_('System'); break; case 'modified': $v = $this->_('Modified'); break; case 'error': $v = $this->_('Error'); break; + case ',': $v = $this->_(','); break; // Comma } $level--; diff --git a/wire/modules/LanguageSupport/ProcessLanguageTranslator.module b/wire/modules/LanguageSupport/ProcessLanguageTranslator.module index d8eabcf9..72d1e52f 100644 --- a/wire/modules/LanguageSupport/ProcessLanguageTranslator.module +++ b/wire/modules/LanguageSupport/ProcessLanguageTranslator.module @@ -15,10 +15,12 @@ * @method string executeEdit() * @method processEdit($form, $textdomain, $translations) * + * @property array $extensions Allowed file extensions for translatable files + * * */ -class ProcessLanguageTranslator extends Process { +class ProcessLanguageTranslator extends Process implements ConfigurableModule { public static function getModuleInfo() { return array( @@ -80,6 +82,11 @@ class ProcessLanguageTranslator extends Process { * */ protected $fp = null; + + public function __construct() { + parent::__construct(); + $this->set('extensions', array('php', 'module', 'inc')); + } /** * Initialize the module and setup the variables above @@ -102,6 +109,40 @@ class ProcessLanguageTranslator extends Process { if($this->fp) fclose($this->fp); } + /** + * Set + * + * @param string $key + * @param mixed $value + * @return self + * + */ + public function set($key, $value) { + if($key === 'extensions') return $this->setExtensions($value); + return parent::set($key, $value); + } + + /** + * Set allowed extensions + * + * @param string|array $extensions + * @since 3.0.212 + * + */ + protected function setExtensions($extensions) { + if(!is_array($extensions)) { + $extensions = preg_replace('/[^a-zA-Z0-9 ]/', ' ', (string) $extensions); + $extensions = explode(' ', strtolower(trim($extensions))); + foreach($extensions as $k => $v) { + if(empty($v)) unset($extensions[$k]); + } + } + if(!in_array('php', $extensions)) $extensions[] = 'php'; + if(!in_array('module', $extensions)) $extensions[] = 'module'; + parent::set('extensions', $extensions); + return $this; + } + /** * Set the language used by the translator process and create the new translator for it * @@ -767,6 +808,7 @@ class ProcessLanguageTranslator extends Process { if(!is_dir($path)) throw new WireException(sprintf($this->_('%s does not exist or is not a directory'), $path)); static $level = 0; + $extensions = $this->extensions; if(!$level) { $cacheKey = "files_" . md5($path); @@ -815,7 +857,7 @@ class ProcessLanguageTranslator extends Process { } $ext = $file->getExtension(); - if($ext != 'php' && $ext != 'module' && $ext != 'inc') continue; + if(!in_array(strtolower($ext), $extensions)) continue; $pathname = $file->getPathname(); $text = file_get_contents($pathname); $found = false; @@ -899,5 +941,20 @@ class ProcessLanguageTranslator extends Process { return $abandoned; } + + /** + * Module config + * + * @param InputfieldWrapper $inputfields + * + */ + public function getModuleConfigInputfields(InputfieldWrapper $inputfields) { + $f = $inputfields->InputfieldText; + $f->attr('name', 'extensions'); + $f->label = $this->_('Supported file extensions for translatable files'); + $f->description = $this->_('Enter a space-separated list of file extensions (`php` and `module` are required).'); + $f->val(implode(' ', $this->extensions)); + $inputfields->add($f); + } }