Skip to content

Commit 2588d77

Browse files
committed
MC-35771: Datepicker/calendar control does not use the store locale
1 parent 29bf891 commit 2588d77

File tree

2 files changed

+66
-4
lines changed
  • app/code/Magento/Customer

2 files changed

+66
-4
lines changed

app/code/Magento/Customer/Block/Widget/Dob.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77

88
use Magento\Customer\Api\CustomerMetadataInterface;
99
use Magento\Framework\Api\ArrayObjectSearch;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Json\EncoderInterface;
12+
use Magento\Framework\Locale\Bundle\DataBundle;
13+
use Magento\Framework\Locale\ResolverInterface;
1014

1115
/**
1216
* Customer date of birth attribute block
1317
*
1418
* @SuppressWarnings(PHPMD.DepthOfInheritance)
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1520
*/
1621
class Dob extends AbstractWidget
1722
{
@@ -39,24 +44,42 @@ class Dob extends AbstractWidget
3944
*/
4045
protected $filterFactory;
4146

47+
/**
48+
* JSON Encoder
49+
*
50+
* @var EncoderInterface
51+
*/
52+
private $encoder;
53+
54+
/**
55+
* @var ResolverInterface
56+
*/
57+
private $localeResolver;
58+
4259
/**
4360
* @param \Magento\Framework\View\Element\Template\Context $context
4461
* @param \Magento\Customer\Helper\Address $addressHelper
4562
* @param CustomerMetadataInterface $customerMetadata
4663
* @param \Magento\Framework\View\Element\Html\Date $dateElement
4764
* @param \Magento\Framework\Data\Form\FilterFactory $filterFactory
4865
* @param array $data
66+
* @param EncoderInterface|null $encoder
67+
* @param ResolverInterface|null $localeResolver
4968
*/
5069
public function __construct(
5170
\Magento\Framework\View\Element\Template\Context $context,
5271
\Magento\Customer\Helper\Address $addressHelper,
5372
CustomerMetadataInterface $customerMetadata,
5473
\Magento\Framework\View\Element\Html\Date $dateElement,
5574
\Magento\Framework\Data\Form\FilterFactory $filterFactory,
56-
array $data = []
75+
array $data = [],
76+
?EncoderInterface $encoder = null,
77+
?ResolverInterface $localeResolver = null
5778
) {
5879
$this->dateElement = $dateElement;
5980
$this->filterFactory = $filterFactory;
81+
$this->encoder = $encoder ?? ObjectManager::getInstance()->get(EncoderInterface::class);
82+
$this->localeResolver = $localeResolver ?? ObjectManager::getInstance()->get(ResolverInterface::class);
6083
parent::__construct($context, $addressHelper, $customerMetadata, $data);
6184
}
6285

@@ -377,4 +400,30 @@ public function getFirstDay()
377400
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
378401
);
379402
}
403+
404+
/**
405+
* Get translated calendar config json formatted
406+
*
407+
* @return string
408+
*/
409+
public function getTranslatedCalendarConfigJson(): string
410+
{
411+
$localeData = (new DataBundle())->get($this->localeResolver->getLocale());
412+
$monthsData = $localeData['calendar']['gregorian']['monthNames'];
413+
$daysData = $localeData['calendar']['gregorian']['dayNames'];
414+
415+
return $this->encoder->encode(
416+
[
417+
'closeText' => __('Done'),
418+
'prevText' => __('Prev'),
419+
'nextText' => __('Next'),
420+
'currentText' => __('Today'),
421+
'monthNames' => array_values(iterator_to_array($monthsData['format']['wide'])),
422+
'monthNamesShort' => array_values(iterator_to_array($monthsData['format']['abbreviated'])),
423+
'dayNames' => array_values(iterator_to_array($daysData['format']['wide'])),
424+
'dayNamesShort' => array_values(iterator_to_array($daysData['format']['abbreviated'])),
425+
'dayNamesMin' => array_values(iterator_to_array($daysData['format']['short'])),
426+
]
427+
);
428+
}
380429
}

app/code/Magento/Customer/view/frontend/templates/widget/dob.phtml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ $fieldCssClass = 'field date field-' . $block->getHtmlId();
2727
$fieldCssClass .= $block->isRequired() ? ' required' : '';
2828
?>
2929
<div class="<?= $block->escapeHtmlAttr($fieldCssClass) ?>">
30-
<label class="label" for="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>"><span><?= $block->escapeHtml($block->getStoreLabel('dob')) ?></span></label>
30+
<label class="label" for="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>">
31+
<span><?= $block->escapeHtml($block->getStoreLabel('dob')) ?></span>
32+
</label>
3133
<div class="control customer-dob">
3234
<?= $block->getFieldHtml() ?>
33-
<?php if ($_message = $block->getAdditionalDescription()) : ?>
35+
<?php if ($_message = $block->getAdditionalDescription()): ?>
3436
<div class="note"><?= $block->escapeHtml($_message) ?></div>
3537
<?php endif; ?>
3638
</div>
@@ -42,4 +44,15 @@ $fieldCssClass .= $block->isRequired() ? ' required' : '';
4244
"Magento_Customer/js/validation": {}
4345
}
4446
}
45-
</script>
47+
</script>
48+
49+
<script>
50+
require([
51+
'jquery',
52+
'jquery-ui-modules/datepicker'
53+
], function ( $ ) {
54+
$.extend(true, $, {
55+
calendarConfig: <?= $block->escapeJs($block->getTranslatedCalendarConfigJson()); ?>
56+
});
57+
});
58+
</script>

0 commit comments

Comments
 (0)