Jump to content

[Solved] datetime and multi-language support


Laegnur
 Share

Recommended Posts

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 by Laegnur
Solved
Link to comment
Share on other sites

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:

2082721178_Capturadepantallaen2022-09-2623-52-39.png.6439ecbb42511a778ca64c21ec96a608.png381527223_Capturadepantallaen2022-09-2623-53-33.png.c4c2f95a221419f6a08967958ddbbe76.png206941884_Capturadepantallaen2022-09-2623-51-19.png.fd2b1e80e2e4a6655f436f5ccd705a0b.png

  • Like 2
Link to comment
Share on other sites

  • Laegnur changed the title to [Solved] datetime and multi-language support
  • 1 year later...

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!

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...