Skip to content

Commit

Permalink
Translatable range tooltip #6221
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora authored and bastianallgeier committed Feb 2, 2024
1 parent 589e5bf commit 69810e2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/fields/range.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Kirby\Toolkit\I18n;

return [
'extends' => 'number',
'props' => [
Expand All @@ -18,6 +20,13 @@
* Enables/disables the tooltip and set the before and after values
*/
'tooltip' => function ($tooltip = true) {
if (is_array($tooltip) === true) {
$after = $tooltip['after'] ?? null;
$before = $tooltip['before'] ?? null;
$tooltip['after'] = I18n::translate($after, $after);
$tooltip['before'] = I18n::translate($before, $before);
}

return $tooltip;
},
]
Expand Down
46 changes: 46 additions & 0 deletions tests/Form/Fields/RangeFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kirby\Form\Fields;

use Kirby\Toolkit\I18n;

class RangeFieldTest extends TestCase
{
public function testDefaultProps()
Expand Down Expand Up @@ -40,4 +42,48 @@ public function testMax()
$this->assertFalse($field->isValid());
$this->assertArrayHasKey('max', $field->errors());
}

public function testTooltip()
{
$field = $this->field('range', [
'tooltip' => [
'before' => 'per',
'after' => 'months'
]
]);

$tooltip = $field->tooltip();
$this->assertIsArray($tooltip);
$this->assertSame('per', $tooltip['before']);
$this->assertSame('months', $tooltip['after']);
}

public function testTooltipTranslation()
{
$props = [
'tooltip' => [
'before' => [
'en' => 'per',
'de' => 'pro'
],
'after' => [
'en' => 'months',
'de' => 'monate'
]
]
];

I18n::$locale = 'en';
$tooltip = $this->field('range', $props)->tooltip();
$this->assertIsArray($tooltip);
$this->assertSame('per', $tooltip['before']);
$this->assertSame('months', $tooltip['after']);


I18n::$locale = 'de';
$tooltip = $this->field('range', $props)->tooltip();
$this->assertIsArray($tooltip);
$this->assertSame('pro', $tooltip['before']);
$this->assertSame('monate', $tooltip['after']);
}
}

0 comments on commit 69810e2

Please sign in to comment.