Laegnur Posted September 26, 2022 Posted September 26, 2022 (edited) Hello On my project, I'm using the multi-language module to translate the site to 3 languages. Now I want to format the posts datetime on a custom way, and I found this method using date() to output weekday name, month name, and day number. But the output names are on English. What is the best way to set the locale for each language added in the multi-language module? $data = strtotime($publicacion->data_publicacion); $contido .= '<em>' . date('l', $data) . '</em>' . "\n"; $contido .= '<strong>' . date('F', $data) . '</strong>' . "\n"; $contido .= '<span>' . date('d', $data) . '</span>' . "\n"; Edited September 26, 2022 by Laegnur Solved
Laegnur Posted September 26, 2022 Author Posted September 26, 2022 Hello @bernhard thanks for your answer. Now I understand how to set the locale for each language in the module and was able to achieve what I wanted. Finally my date code would look like this $data = strtotime($publicacion->data_publicacion); $dateFormatter = \IntlDateFormatter::create( $languages->getLocale(), \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, \date_default_timezone_get(), \IntlDateFormatter::GREGORIAN ); $contido .= '<time datetime="' . $publicacion->data_publicacion . '">' ."\n"; $dateFormatter->setPattern('EEEE'); $contido .= '<em>' . datefmt_format($dateFormatter, $data) . '</em>' . "\n"; $dateFormatter->setPattern('MMMM'); $contido .= '<strong>' . datefmt_format($dateFormatter, $data) . '</strong>' . "\n"; $dateFormatter->setPattern('dd'); $contido .= '<span>' . datefmt_format($dateFormatter, $data) . '</span>' . "\n"; $contido .= '</time>' . "\n"; And the final result: 2
sz-ligatur Posted November 6, 2023 Posted November 6, 2023 Hello @ryan, I'm using a datetimefield (in a frontend form), input type is set to separate select inputs. In a multilanguage context I didn't manage to get the translated month other than english. I found that in wire/modules/Inputfield/InputfieldDatetime/types/InputfieldDatetimeSelect.php there is strftime() in use. As this is marked as deprecated for PHP 8.1 I tried to use IntlDateFormatter() like the following: $abbreviate = strpos($format, 'M') === false; // line 90 $fmt = new \IntlDateFormatter( $this->wire('languages')->getLocale(), \IntlDateFormatter::FULL, \IntlDateFormatter::FULL ); $monthFormat = $abbreviate ? 'LLL' : 'LLLL'; $fmt->setPattern($monthFormat); for($n = 1; $n <= 12; $n++) { //$monthFormat = $abbreviate ? '%b' : '%B'; //replaced //$monthLabel = $sanitizer->entities($datetime->strftime($monthFormat, mktime(0, 0, 0, $n, 1))); //replaced $monthLabel = $sanitizer->entities($fmt->format(mktime(0, 0, 0, $n, 1))); $months->addOption($n, $monthLabel); } That did it so far, it outputs the correct language variant - but: - sure, I don't want to hack core files… is there a way to replace the whole render() method of class InputfieldDatetimeSelect with a hook? - I’m not really familiar with the IntlDateFormatter(), is the usage correct here? - are you maybe willing to update the core file accordingly? Thank you!
da² Posted November 6, 2023 Posted November 6, 2023 Hello @sz-ligatur Months are now translatable in wire/core/WireDataTime : https://github.com/processwire/processwire-issues/issues/1774#issuecomment-1618267798 1
sz-ligatur Posted November 7, 2023 Posted November 7, 2023 11 hours ago, da² said: Hello @sz-ligatur Months are now translatable in wire/core/WireDataTime : https://github.com/processwire/processwire-issues/issues/1774#issuecomment-1618267798 Oha, great, I missed that – thanks a lot! 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now