Huge thanks for this technique. I decided to go for tomek's as I'm not sure whether the locale setting would be for all users, and as the site I'm building is multilingual I can't have one website visitor changing the locale for another. My uncertainty is because as far as I'm aware locale is a php-wide setting but I'd appreciate correction.
I wanted to centralize my date snippet rather than have one per language, so I used a language parameter and switch statement. I have another snippet which returns a string determining the current page's language (Eng or Esp, by looking up the article hierarchy - contexts were too complex and fine grained for my use case so I achieved the same just by having two document hierarchies and a template variable linking articles of different languages).
My date conversion snippet (the server locale is UK so I only need convert to Espanol):
<?php
//SetLocalDate
//Converts date format
//Use: [[snTranslateDate? &date=`[[*publishedon]]` &language=`[[snLanguageAbbrev]]`]]
if ($language === 'Esp') {
// months on Spanish language
$months = array(1 => "Enero", //January
2 => "Febrero", //February
3 => "Marzo", //March
4 => "Abril", //April
5 => "Mayo", //May
6 => "Junio", //Jun
7 => "Julio", //July
8 => "Agosto", //August
9 => "Septiembre", //September
10=> "Octubre", //October
11=> "Noviembre", //November
12=> "Diciembre"); //December
$mj = date("n", strtotime($date));
$month = $months[$mj]; // For example 1
$day = date("d", strtotime($date)); //For example 28
$year = date("Y", strtotime($date));// For example 2012
//display date format for example:
//Septiembre 28, 2013
return $month.' '.$day.', '.$year;
} else {
return strftime('%B %d, %Y', strtotime($date));
}
My language snippet (it would be nicer if the ID wasn't hardcoded) - the code traverses up the tree to the second level in the hierarchy, irrelevant of how deep the current article is. Article 30 is the container resource for the Spanish language resources:
<?php
$id = $modx->resource->get('id');
$parentids = $modx->getParentIds($id);
$levelcount = count($parentids);
// doc id 30 = Esp container document
if ($parentids[($levelcount - 2)] === 30) {
return "Esp";
} else {
return "Eng";
};
The snippet call (a latest posts template for the Articles extra, hence using + instead of * for the variable):
[[+publishedon:notempty=`[[snTranslateDate? &date=`[[+publishedon]]` &language=`[[snLanguageAbbrev]]`]]`]]
Always nice to see different approaches so thought I'd post in case it fuels someone else's creativity in some way, as the above did me, big thanks.