Skip to content

Commit

Permalink
Merge 0ac02b0 into fbd3764
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Feb 8, 2019
2 parents fbd3764 + 0ac02b0 commit 82cab58
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 40 deletions.
12 changes: 11 additions & 1 deletion assets/translate.less
Expand Up @@ -61,6 +61,11 @@ body.translate {
.language-selectors {
display: flex;
align-items: center;
> * {
// Get rid of the margin from the following three elements, so the .language-selectors can be treated the
// same as the .buttons element (for vertical positioning).
margin-bottom: 0;
}
.source-lang-widget {
flex: 2;
text-align: left;
Expand All @@ -80,7 +85,12 @@ body.translate {
}

.oo-ui-fieldLayout {
margin-bottom: 1.5rem;
margin-bottom: 0.75rem;
}
fieldset:not(:first-child) {
margin-top: 0;
padding-top: 0.75rem;
border-top: 1px solid @wmui-color-base80;
}
.source-lang-not-found label {
font-style: italic;
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/assets/entrypoints.json
Expand Up @@ -2,7 +2,7 @@
"entrypoints": {
"app": {
"css": [
"assets/app.cdb1250c.css"
"assets/app.1709d2c7.css"
],
"js": [
"assets/app.ae00a295.js"
Expand Down
2 changes: 1 addition & 1 deletion public/assets/manifest.json
@@ -1,5 +1,5 @@
{
"assets/app.css": "assets/app.cdb1250c.css",
"assets/app.css": "assets/app.1709d2c7.css",
"assets/app.js": "assets/app.ae00a295.js",
"assets/grabbing.cur": "assets/a8c874b93b3d848f39a71260c57e3863.cur",
"assets/grab.cur": "assets/b06c243f534d9c5461d16528156cd5a8.cur",
Expand Down
39 changes: 8 additions & 31 deletions src/Controller/TranslateController.php
Expand Up @@ -5,15 +5,14 @@

use App\Model\Svg\SvgFile;
use App\Model\Title;
use App\OOUI\TranslationsFieldset;
use App\Service\FileCache;
use App\Service\Uploader;
use Krinkle\Intuition\Intuition;
use OOUI\ButtonInputWidget;
use OOUI\DropdownInputWidget;
use OOUI\FieldLayout;
use OOUI\HorizontalLayout;
use OOUI\LabelWidget;
use OOUI\TextInputWidget;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -124,41 +123,19 @@ public function translate(
'classes' => ['language-selectors'],
]);

// Messages.
// Form fields for translation messages are in a fieldset which contains fieldsets for each group of messages.
$translations = $svgFile->getInFileTranslations();
$formFields = [];
foreach ($translations as $tspanId => $translation) {
// 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, '', $translation[$sourceLang->getValue()]['text']);
if ('' === $sourceLabel) {
continue;
}
// Add fields for all other translations.
$fieldValue = isset($translation[$targetLang->getValue()])
? $translation[$targetLang->getValue()]['text']
: '';
$inputWidget = new TextInputWidget([
'name' => $tspanId,
'value' => $fieldValue,
'data' => ['tspan-id' => $tspanId],
]);
$field = new FieldLayout(
$inputWidget,
[
'label' => $sourceLabel,
'infusable' => true,
]
);
$formFields[] = $field;
}
$translationsFieldset = new TranslationsFieldset([
'translations' => $translations,
'source_lang_code' => $sourceLang->getValue(),
'target_lang_code' => $targetLang->getValue(),
]);

return $this->render('translate.html.twig', [
'page_class' => 'translate',
'title' => Title::text($filename),
'filename' => $normalizedFilename,
'fields' => $formFields,
'translation_fieldset' => $translationsFieldset,
'download_button' => $downloadButton,
'upload_button' => $uploadButton,
'language_selectors' => $languageSelectorsLayout,
Expand Down
67 changes: 67 additions & 0 deletions src/OOUI/TranslationsFieldset.php
@@ -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]);
}
}
6 changes: 1 addition & 5 deletions templates/translate.html.twig
Expand Up @@ -31,11 +31,7 @@
<input type="hidden" name="target-lang" value="{{ target_lang }}" />
<div class="form-column">
{{ language_selectors|raw }}
<div class="translation-fields">
{% for field in fields %}
{{ field|raw }}
{% endfor %}
</div>
<div class="translation-fields">{{ translation_fieldset|raw }}</div>
</div>
<div class="image-column">
<div class="buttons">
Expand Down
48 changes: 48 additions & 0 deletions tests/OOUI/TranslationsFieldsetTest.php
@@ -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],
];
}
}

0 comments on commit 82cab58

Please sign in to comment.