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:
@@ -461,6 +461,16 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
|
||||
$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)) {
|
||||
$type = $rows > 1 ? 'InputfieldTextarea' : 'InputfieldText';
|
||||
} else if(strpos($type, 'Inputfield') !== 0) {
|
||||
@@ -471,15 +481,43 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
|
||||
if(!$field) $field = $this->wire()->modules->get('InputfieldText');
|
||||
if($rows > 1 && $field instanceof InputfieldTextarea) $field->attr('rows', $rows);
|
||||
|
||||
/** @var InputfieldText $field */
|
||||
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|InputfieldSelect $field */
|
||||
$field->attr('id+name', $hash);
|
||||
$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->addClass(strlen($translated) ? 'translated' : 'untranslated', 'wrapClass');
|
||||
$field->addClass('translatable');
|
||||
|
||||
if($comment) {
|
||||
if($field instanceof InputfieldSelect) {
|
||||
if(strpos($comment, '//')) {
|
||||
list($label, $notes) = explode('//', $comment, 2);
|
||||
} else {
|
||||
$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]";
|
||||
@@ -490,6 +528,7 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
|
||||
$field->notes = $comment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($this->language && !$this->language->isDefault()) {
|
||||
foreach($alternates as $altText => $altTranslation) {
|
||||
@@ -523,7 +562,7 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
|
||||
}
|
||||
|
||||
$field->skipLabel = Inputfield::skipLabelHeader;
|
||||
$field->description = $untranslated;
|
||||
if(!strlen($field->description)) $field->description = $untranslated;
|
||||
|
||||
return $field;
|
||||
}
|
||||
@@ -957,4 +996,3 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
|
||||
$inputfields->add($f);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user