Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
public/assets/app.cdb1250c.css → public/assets/app.1709d2c7.css
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace App\OOUI; | ||
|
||
use OOUI\FieldLayout; | ||
use OOUI\FieldsetLayout; | ||
use OOUI\TextInputWidget; | ||
|
||
class TranslationsFieldset extends FieldsetLayout | ||
{ | ||
|
||
/** | ||
* @param array $config Configuration options | ||
* @param FieldLayout[] $config['translations'] Translations to add. | ||
* @param FieldLayout[] $config['source_lang_code'] Source language code. | ||
* @param FieldLayout[] $config['target_lang_code'] Target language code. | ||
*/ | ||
public function __construct(array $config = []) | ||
{ | ||
$currentFieldset = new FieldsetLayout(); | ||
$fieldsets = [$currentFieldset]; | ||
$prevTranslation = null; | ||
foreach ($config['translations'] as $tspanId => $translation) { | ||
$fieldValue = isset($translation[$config['target_lang_code']]) | ||
? $translation[$config['target_lang_code']]['text'] | ||
: ''; | ||
$field = $this->getField($tspanId, $translation[$config['source_lang_code']]['text'], $fieldValue); | ||
if (!$field) { | ||
continue; | ||
} | ||
// Start a new fieldset if the current translation's parent is different to the previous's. | ||
if ($prevTranslation | ||
&& $prevTranslation['fallback']['data-parent'] !== $translation['fallback']['data-parent']) { | ||
$currentFieldset = new FieldsetLayout(); | ||
$fieldsets[] = $currentFieldset; | ||
} | ||
$currentFieldset->addItems([$field]); | ||
$prevTranslation = $translation; | ||
} | ||
parent::__construct(['items' => $fieldsets]); | ||
} | ||
|
||
/** | ||
* Get a field for a single translation string. | ||
* @param string $tspanId The field name. | ||
* @param string $label The field label. | ||
* @param string $value The field value. | ||
* @return FieldLayout|bool The field, or false if the label is empty. | ||
*/ | ||
protected function getField(string $tspanId, string $label, string $value) | ||
{ | ||
// Do not display translations that are only white-space. https://stackoverflow.com/a/4167053/99667 | ||
// @TODO SvgFile should probably be handling this for us. | ||
$whitespacePattern = '/^[\pZ\pC]+|[\pZ\pC]+$/u'; | ||
$sourceLabel = preg_replace($whitespacePattern, '', $label); | ||
if ('' === $sourceLabel) { | ||
return false; | ||
} | ||
$inputWidget = new TextInputWidget([ | ||
'name' => $tspanId, | ||
'value' => $value, | ||
'data' => ['tspan-id' => $tspanId], | ||
]); | ||
return new FieldLayout($inputWidget, ['label' => $sourceLabel, 'infusable' => true]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace App\Tests\OOUI; | ||
|
||
use App\OOUI\TranslationsFieldset; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TranslationsFieldsetTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider fieldsetGroupingProvider() | ||
*/ | ||
public function testFieldsetGrouping(array $translations, int $fieldsetCount): void | ||
{ | ||
$fieldset = new TranslationsFieldset([ | ||
'translations' => $translations, | ||
'source_lang_code' => 'fallback', | ||
'target_lang_code' => 'eo', | ||
]); | ||
static::assertCount($fieldsetCount, $fieldset->getItems()); | ||
} | ||
|
||
/** | ||
* @return string[][][] | ||
*/ | ||
public function fieldsetGroupingProvider(): array | ||
{ | ||
$singleField = [ | ||
'span1' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text1']], | ||
]; | ||
$twoUnrelatedFields = [ | ||
'span1' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text1']], | ||
'span2' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text2']], | ||
]; | ||
$threeFieldsInTwoGroups = [ | ||
'span1' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text1']], | ||
'span2A' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text2']], | ||
'span2B' => ['fallback' => ['text' => 'Hello', 'data-parent' => 'text2']], | ||
]; | ||
return [ | ||
'single field' => [$singleField, 1], | ||
'two unrelated fields' => [$twoUnrelatedFields, 2], | ||
'three fields in two groups' => [$threeFieldsInTwoGroups, 2], | ||
]; | ||
} | ||
} |