<![CDATA[ How to set a specific locale for the :date output filter? - My Forums]]> https://forums.modx.com/thread/?thread=82522 <![CDATA[How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455386 pubDate element conform to RFC822 date and time format which uses names for day of the week and month. In multilingual site that is a problem, as MODX's cultureKey respects the site/context language which results in invalid pubDate format.

RFC822 requires this:
<pubDate>Thu, 14 Feb 2013 13:15:00 +0100</pubDate>
But for Polish locale I get this:
<pubDate>czw, 14 lut 2013 13:15:00 +0100</pubDate>


The question is: how to set locale for :date output filter?

I'd expect something like this should be possible:
<pubDate>[[+publishedon:strtotime:locale=`en_US`:date=`%a, %d %b %Y %H:%M:%S %z`]]</pubDate>


I followed instructions in getResources.Building a RSS feed from MODX docs.]]>
gadamiak Feb 16, 2013, 03:42 AM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455386
<![CDATA[Re: How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-506866
I created an output modifier to solve this which seems way easier than

Snippet Code "dateLocale":
<?php
setlocale(LC_ALL, 'en_US');

return strftime($options,$input);


Usage:
[[+publishedon:strtotime:dateLocale=`%a, %d %b %Y %H:%M:%S %z`]]


I use it in a RSS-feed to get the correct format for the date.]]>
raadhuis Aug 20, 2014, 01:51 AM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-506866
<![CDATA[Re: How to set a specific locale for the :date output filter? (Best Answer)]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-456820
<?php
/*
 * (Re)Sets locale
 *
 * Set locale for time to `en_US.utf8`:
 *
 *   [[locale? &category=`LC_TIME` &locale=`en_US.utf8`]]
 *   [[...:locale=`category==LC_TIME||locale==en_US.utf8`:...]]
 *
 * Reset to system locale for all categories:
 *
 *   [[locale]]
 *
 * Params:
 * - category: a named constant specifying the category of the functions 
 * affected by the locale setting
 * - locale: a locale to set, eg. "en_US.utf8"
 */

/* Initialize */
(string) $category = $modx->getOption('category', $scriptProperties, 'LC_ALL');
$config = $modx->getConfig();
(string) $locale = ($locale = $modx->getOption('locale', $scriptProperties, '')) ?
  $locale : $syslocale = $config['locale'];
unset($config);

/* If used as output filter */
if (isset($input)) {
  $modx->getService('parser', 'modParser');
  $options = $modx->parser->parseProperties($options);
  $locale = (array_key_exists('locale', $options)) ?
    (string) $options['locale'] : $locale;
  $category = (array_key_exists('category', $options)) ?
    (string) strtoupper($options['category']) : $category;
  unset($options);
}

setlocale($category, $locale);

if (isset($input))
  return $input;

return;


I used it before getResources call setting LC_TIME to en_US.utf8 and resetting it to system setting after that call. One thing worth noting is the proper transcription of the locale, which must match what is installed on the host OS.

The snippet above works fine except output filter mode, which I couldn't manage to work.]]>
gadamiak Feb 27, 2013, 11:18 AM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-456820
<![CDATA[Re: How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455486 Quote from: gadamiak at Feb 17, 2013, 03:35 PM
@BobRay: The question is not about the timestamp content but it's format. The problem is in names (not numbers!) used in the time format.

Timestamps only have one format (seconds since Jan 01 1970), so the problem is not about the timestamp format, it's about the format you get when you convert a timestamp to a more readable format. That said, I think you're right that the method I suggested won't work with MODX output modifiers.

If the author is right, date() apparently infers the locale from the format of the date string, so in a custom snippet you could convert the timestamp it to a string with strftime, parse it, then go back the other way with strtotime() and date(), but I'm sure there's an easier way to do what you want.

You're not on a Windows server, by any chance? That changes things.]]>
BobRay Feb 17, 2013, 04:41 PM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455486
<![CDATA[Re: How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455454
@Michael: I tried that but to no avail. Looks like the locale setting gets reset by parser in the meantime.

Here's my quick snippet:

<?php
/*
 * (Re)Sets locale
 *
 * Usage
 * - [[locale? &category=`LC_TIME` &locale=`en_US`]]
 * - [[...:locale=`category==LC_TIME||locale==en_US`:...]]
 *
 * When called without &locale will reset it to system setting
 *
 * Params:
 * - category: a named constant specifying the category of the functions 
 * affected by the locale setting
 * - locale: a locale to set, eg. "en_US"
 */

/* Initialize */
$category = $modx->getOption('category', $scriptProperties, 'LC_ALL');
$config = $modx->getConfig();
$locale = ($locale = $modx->getOption('locale', $scriptProperties, '')) ? $locale : $config['locale'];
unset($config);

/* If used as output filter */
if (isset($input)) {
  $modx->getService('parser', 'modParser');
  $options = $modx->parser->parseProperties($options);
  $locale = (array_key_exists('locale', $options)) ? $options['locale'] : $locale;
  $category = (array_key_exists('category', $options)) ? $options['category'] : $category;
  unset($options);
}

return setlocale($category, $locale);


I used it before and after getResources call. But it didn't work and locale doesn't change. You may check current setting with [[locale? &category=`LC_TIME` &locale=`0`]]. I used it in getResources tpl chunk, but it didn't work either. The same with output filter.

Looks like I have to write my custom output filter, which accepts both format for strftime and a locale.=]]>
gadamiak Feb 17, 2013, 09:35 AM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455454
<![CDATA[Re: How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455441 So it would be easiest to just make an small snippet, setting the right locale just for this document. Instead of setting it hundreds of times in the getResuoces rows.

<?php
setlocale(LC_TIME, 'en_US');


And call it before getResouces.]]>
m.engel Feb 17, 2013, 05:21 AM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455441
<![CDATA[Re: How to set a specific locale for the :date output filter?]]> https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455433
you can't really set a locale for strtotime. If you're American, you see 11/12/10 and think "12 November, 2010". If you're Australian (or European), you think it's 11 December, 2010. If you're a sysadmin who reads in ISO, it looks like 10th December 2011.

The best way to compensate for this is by modifying your joining characters. Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.

Observe:

<?php
echo date("jS F, Y", strtotime("11.12.10"));
// outputs 10th December, 2011

echo date("jS F, Y", strtotime("11/12/10"));
// outputs 12th November, 2010

echo date("jS F, Y", strtotime("11-12-10"));
// outputs 11th December, 2010
?>

All the output modifier does is call strtotime, so in theory, it should work. If not, you'd have to write your own custom output modifier.]]>
BobRay Feb 16, 2013, 09:21 PM https://forums.modx.com/thread/82522/how-to-set-a-specific-locale-for-the-date-output-filter#dis-post-455433