1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +02:00

Add support for InputfieldSelect and InputfieldRadios that can be specified in language translation comment, i.e. __('Red'); // Color to use for this language? options=[Red,Blue,Green] type=radios

This commit is contained in:
Ryan Cramer
2023-06-23 10:55:14 -04:00
parent 73ab10658c
commit a01b922efb

View File

@@ -461,6 +461,16 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
$rows = 3; $rows = 3;
} }
if(stripos($comment, 'options=[') !== false && preg_match('!\boptions=\[([^\]]+)\]!', $comment, $matches)) {
// Options for select or radios
// i.e. "options=[r,b,g]" or "options[r:Red,b:Blue,g:Green]"
$c = strpos($matches[1], '|') ? '|' : ',';
$options = explode($c, $matches[1]);
$comment = str_replace($matches[0], '', $comment);
} else {
$options = array();
}
if(empty($type)) { if(empty($type)) {
$type = $rows > 1 ? 'InputfieldTextarea' : 'InputfieldText'; $type = $rows > 1 ? 'InputfieldTextarea' : 'InputfieldText';
} else if(strpos($type, 'Inputfield') !== 0) { } else if(strpos($type, 'Inputfield') !== 0) {
@@ -470,24 +480,53 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
$field = $this->wire()->modules->get($type); $field = $this->wire()->modules->get($type);
if(!$field) $field = $this->wire()->modules->get('InputfieldText'); if(!$field) $field = $this->wire()->modules->get('InputfieldText');
if($rows > 1 && $field instanceof InputfieldTextarea) $field->attr('rows', $rows); if($rows > 1 && $field instanceof InputfieldTextarea) $field->attr('rows', $rows);
if(count($options) && $field instanceof InputfieldSelect) {
foreach($options as $option) {
if(strpos($option, ':') !== false) {
// separate "value:label"
list($option, $label) = explode(':', $option, 2);
$field->addOption(trim($option), trim($label));
} else {
// just "value"
$field->addOption(trim($option));
}
}
}
/** @var InputfieldText $field */ /** @var InputfieldText|InputfieldSelect $field */
$field->attr('id+name', $hash); $field->attr('id+name', $hash);
$field->set('textFormat', Inputfield::textFormatNone); $field->set('textFormat', Inputfield::textFormatNone);
$field->attr('value', $translated); if($field instanceof InputfieldSelect) {
$v = strlen($translated) ? $translated : $untranslated;
$field->val($v);
} else {
$field->val($translated);
}
$field->label = $untranslated; $field->label = $untranslated;
$field->addClass(strlen($translated) ? 'translated' : 'untranslated', 'wrapClass'); $field->addClass(strlen($translated) ? 'translated' : 'untranslated', 'wrapClass');
$field->addClass('translatable'); $field->addClass('translatable');
if($comment) { if($comment) {
if(preg_match('{^(.*?)//(.*)$}', $comment, $m)) { if($field instanceof InputfieldSelect) {
if(trim($m[1]) != trim($untranslated)) { if(strpos($comment, '//')) {
$field->notes = "$m[1]\n$m[2]"; list($label, $notes) = explode('//', $comment, 2);
} else { } else {
$field->notes = $m[2]; $label = $comment;
$notes = '';
}
$field->description = $label;
$field->notes = $notes;
} else {
if(preg_match('{^(.*?)//(.*)$}', $comment, $m)) {
if(trim($m[1]) != trim($untranslated)) {
$field->notes = "$m[1]\n$m[2]";
} else {
$field->notes = $m[2];
}
} else if(trim($comment) != trim($untranslated)) {
$field->notes = $comment;
} }
} else if(trim($comment) != trim($untranslated)) {
$field->notes = $comment;
} }
} }
@@ -523,7 +562,7 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
} }
$field->skipLabel = Inputfield::skipLabelHeader; $field->skipLabel = Inputfield::skipLabelHeader;
$field->description = $untranslated; if(!strlen($field->description)) $field->description = $untranslated;
return $field; return $field;
} }
@@ -957,4 +996,3 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
$inputfields->add($f); $inputfields->add($f);
} }
} }