1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-10 00:37:02 +02:00

Add feature request processwire/processwire-requests#480 to support other file extensions for translatable files in ProcessLanguageTranslator.module as a module config setting

This commit is contained in:
Ryan Cramer
2023-02-16 09:21:53 -05:00
parent b155596089
commit a716232172
2 changed files with 60 additions and 2 deletions

View File

@@ -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--;

View File

@@ -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);
}
}