diff --git a/config/assets.php b/config/assets.php index 9cdb6af4be9..632359abf7a 100644 --- a/config/assets.php +++ b/config/assets.php @@ -188,8 +188,8 @@ |-------------------------------------------------------------------------- | | Configure the aspect ratio presets available in the Control Panel image - | crop editor. Ratios may be provided as "W:H" strings, keyed values, or - | arrays with custom labels and ratio values. + | crop editor. Each entry may be a "W:H" string (e.g. "16:9") or an array + | with a custom label and ratio: ['label' => 'Wide', 'ratio' => '16:9']. | */ diff --git a/src/Assets/CropAspectRatios.php b/src/Assets/CropAspectRatios.php index b6642fa7a7a..e9905c34d0d 100644 --- a/src/Assets/CropAspectRatios.php +++ b/src/Assets/CropAspectRatios.php @@ -4,6 +4,8 @@ use Illuminate\Support\Facades\Log; +use function Statamic\trans as __; + class CropAspectRatios { /** @@ -39,7 +41,6 @@ private static function parse(mixed $entry, mixed $key): ?array return null; } - $label = is_string($key) ? $key : (string) $entry; $value = self::ratioToFloat($entry); if ($value === null) { @@ -49,7 +50,7 @@ private static function parse(mixed $entry, mixed $key): ?array } return [ - 'label' => $label, + 'label' => __((string) $entry), 'value' => $value, ]; } @@ -60,7 +61,7 @@ private static function parse(mixed $entry, mixed $key): ?array */ private static function parseArrayEntry(array $entry, mixed $key): ?array { - $label = $entry['label'] ?? (is_string($key) ? $key : null); + $label = $entry['label'] ?? null; $ratio = $entry['ratio'] ?? null; $labelIsStringable = is_string($label) || is_numeric($label) || $label instanceof \Stringable; @@ -80,7 +81,7 @@ private static function parseArrayEntry(array $entry, mixed $key): ?array } return [ - 'label' => (string) $label, + 'label' => __((string) $label), 'value' => $value, ]; } diff --git a/tests/Assets/CropAspectRatiosTest.php b/tests/Assets/CropAspectRatiosTest.php index 6163527be4b..fcf41c4022b 100644 --- a/tests/Assets/CropAspectRatiosTest.php +++ b/tests/Assets/CropAspectRatiosTest.php @@ -38,16 +38,20 @@ public function it_supports_custom_labels_and_fractional_ratio_strings() } #[Test] - public function it_supports_keyed_ratio_entries() + public function it_translates_labels() { + app('translator')->addLines([ + 'crop.wide' => 'Wide Screen', + ], 'en'); + config()->set('statamic.assets.crop_aspect_ratios', [ - 'Portrait' => '9:16', - 'A4' => '210:297', + ['label' => 'crop.wide', 'ratio' => '16:9'], + ['label' => 'Square', 'ratio' => '1:1'], ]); $this->assertSame([ - ['label' => 'Portrait', 'value' => 9 / 16], - ['label' => 'A4', 'value' => 210 / 297], + ['label' => 'Wide Screen', 'value' => 16 / 9], + ['label' => 'Square', 'value' => 1.0], ], CropAspectRatios::all()); } diff --git a/tests/Http/View/Composers/JavascriptComposerTest.php b/tests/Http/View/Composers/JavascriptComposerTest.php index 7419571e247..f37ab390211 100644 --- a/tests/Http/View/Composers/JavascriptComposerTest.php +++ b/tests/Http/View/Composers/JavascriptComposerTest.php @@ -17,7 +17,7 @@ class JavascriptComposerTest extends TestCase public function it_provides_crop_aspect_ratios_to_script_config() { config()->set('statamic.assets.crop_aspect_ratios', [ - 'Portrait' => '9:16', + '9:16', ['label' => 'US Letter', 'ratio' => '8.5:11'], ]); @@ -33,7 +33,7 @@ public function it_provides_crop_aspect_ratios_to_script_config() $json = Statamic::jsonVariables(request()); $this->assertSame([ - ['label' => 'Portrait', 'value' => 9 / 16], + ['label' => '9:16', 'value' => 9 / 16], ['label' => 'US Letter', 'value' => 8.5 / 11], ], $json['cropAspectRatios']); }