We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23560
    • 2 Posts
    Hey everyone, I added a new output filter to modx core that allows localized date strings. I’ve submit it for inclusion into core but I thought I’d share it with you all as I think it’s fairly handy.

    You will need to edit the source to add functionality:
    ///////////  Add this to core/model/modx/filters/modoutputfilter.class.php near the other date functions ///////////
    
    case 'dateloc':
    	/* See PHP's strftime - http://www.php.net/manual/en/function.strftime.php */
    	if (empty($this->modx->lexicon)) $this->modx->getService('lexicon','modLexicon');
    	$this->modx->lexicon->load('filters');
    	$ori_name = array('/\bMon\b/','/\bTue\b/','/\bWed\b/','/\bThu\b/','/\bFri\b/','/\bSat\b/','/\bSun\b/','/\bMonday\b/','/\bTuesday\b/','/\bWednesday\b/','/\bThursday\b/','/\bFriday\b/','/\bSaturday\b/','/\bSunday\b/','/\bJan\b/','/\bFeb\b/','/\bMar\b/','/\bApr\b/','/\bMay\b/','/\bJun\b/','/\bJul\b/','/\bAug\b/','/\bSep\b/','/\bOct\b/','/\bNov\b/','/\bDec\b/','/\bJanuary\b/','/\bFebruary\b/','/\bMarch\b/','/\bApril\b/','/\bMay\b/','/\bJune\b/','/\bJuly\b/','/\bAugust\b/','/\bSeptember\b/','/\bOctober\b/','/\bNovember\b/','/\bDecember\b/','/\bAM\b/','/\bPM\b/');
    	$loc_name = array($this->modx->lexicon('mon'),$this->modx->lexicon('tue'),$this->modx->lexicon('wed'),$this->modx->lexicon('thu'),$this->modx->lexicon('fri'),$this->modx->lexicon('sat'),$this->modx->lexicon('sun'),$this->modx->lexicon('monday'),$this->modx->lexicon('tuesday'),$this->modx->lexicon('wednesday'),$this->modx->lexicon('thursday'),$this->modx->lexicon('friday'),$this->modx->lexicon('saturday'),$this->modx->lexicon('sunday'),$this->modx->lexicon('jan'),$this->modx->lexicon('fev'),$this->modx->lexicon('mar'),$this->modx->lexicon('apr'),$this->modx->lexicon('may'),$this->modx->lexicon('jun'),$this->modx->lexicon('jul'),$this->modx->lexicon('aug'),$this->modx->lexicon('sep'),$this->modx->lexicon('oct'),$this->modx->lexicon('nov'),$this->modx->lexicon('dec'),$this->modx->lexicon('january'),$this->modx->lexicon('february'),$this->modx->lexicon('march'),$this->modx->lexicon('april'),$this->modx->lexicon('may'),$this->modx->lexicon('june'),$this->modx->lexicon('july'),$this->modx->lexicon('august'),$this->modx->lexicon('september'),$this->modx->lexicon('october'),$this->modx->lexicon('november'),$this->modx->lexicon('december'),$this->modx->lexicon('AM'),$this->modx->lexicon('PM'));
    	if (empty($m_val))
    		$m_val = "%A, %d %B %Y %H:%M:%S"; /* @todo this should be modx default date/time format? */
    	$value = 0 + $output;
    	if ($value != 0 && $value != -1) {
    		$output= preg_replace($ori_name,$loc_name,strftime($m_val, 0 + $output));
    	} else {
    		$output= '';
    	}
    	break;

    ///////////  Add this to core/lexicon/en/filters.inc.php ///////////
    
    /* Localized date output filter names :dateloc */
      /* %a */
    $_lang['mon'] = 'Mon';
    $_lang['tue'] = 'Tue';
    $_lang['wed'] = 'Wed';
    $_lang['thu'] = 'Thu';
    $_lang['fri'] = 'Fri';
    $_lang['sat'] = 'Sat';
    $_lang['sun'] = 'Sun';
      /* %A */
    $_lang['monday'] = 'Monday';
    $_lang['tuesday'] = 'Tuesday';
    $_lang['wednesday'] = 'Wednesday';
    $_lang['thursday'] = 'Thursday';
    $_lang['friday'] = 'Friday';
    $_lang['saturday'] = 'Saturday';
    $_lang['sunday'] = 'Sunday';
      /* %b and %h */
    $_lang['jan'] = 'Jan';
    $_lang['feb'] = 'Feb';
    $_lang['mar'] = 'Mar';
    $_lang['apr'] = 'Apr';
    $_lang['may'] = 'May';
    $_lang['jun'] = 'Jun';
    $_lang['jul'] = 'Jul';
    $_lang['aug'] = 'Aug';
    $_lang['sep'] = 'Sep';
    $_lang['oct'] = 'Oct';
    $_lang['nov'] = 'Nov';
    $_lang['dec'] = 'Dec';
      /* %B */
    $_lang['january'] = 'January';
    $_lang['february'] = 'February';
    $_lang['march'] = 'March';
    $_lang['april'] = 'April';
    $_lang['may'] = 'May';
    $_lang['june'] = 'June';
    $_lang['july'] = 'July';
    $_lang['august'] = 'August';
    $_lang['september'] = 'September';
    $_lang['october'] = 'October';
    $_lang['november'] = 'November';
    $_lang['december'] = 'December';
      /* Other */
    $_lang['AM'] = 'AM';
    $_lang['PM'] = 'PM';

    ///////////  Add this to core/lexicon/fr/filters.inc.php ///////////
    
    /* Localized date output filter names :dateloc */
      /* %a */
    $_lang['mon'] = 'Lun';
    $_lang['tue'] = 'Mar';
    $_lang['wed'] = 'Mer';
    $_lang['thu'] = 'Jeu';
    $_lang['fri'] = 'Ven';
    $_lang['sat'] = 'Sam';
    $_lang['sun'] = 'Dim';
      /* %A */
    $_lang['monday'] = 'Lundi';
    $_lang['tuesday'] = 'Mardi';
    $_lang['wednesday'] = 'Mercredi';
    $_lang['thursday'] = 'Jeudi';
    $_lang['friday'] = 'Vendredi';
    $_lang['saturday'] = 'Samedi';
    $_lang['sunday'] = 'Dimanche';
      /* %b and %h */
    $_lang['jan'] = 'Jan';
    $_lang['feb'] = 'Fév';
    $_lang['mar'] = 'Mar';
    $_lang['apr'] = 'Avr';
    $_lang['may'] = 'Mai';
    $_lang['jun'] = 'Jun';
    $_lang['jul'] = 'Jul';
    $_lang['aug'] = 'Aoû';
    $_lang['sep'] = 'Sep';
    $_lang['oct'] = 'Oct';
    $_lang['nov'] = 'Nov';
    $_lang['dec'] = 'Déc';
      /* %B */
    $_lang['january'] = 'Janvier';
    $_lang['february'] = 'Février';
    $_lang['march'] = 'Mars';
    $_lang['april'] = 'Avril';
    $_lang['may'] = 'Mai';
    $_lang['june'] = 'Juin';
    $_lang['july'] = 'Juillet';
    $_lang['august'] = 'Août';
    $_lang['september'] = 'Septembre';
    $_lang['october'] = 'Octobre';
    $_lang['november'] = 'Novembre';
    $_lang['december'] = 'Décembre';
      /* Other */
    $_lang['AM'] = 'AM';
    $_lang['PM'] = 'PM';


    Usage

    Usage is the same as the :date output filter, just use :dateloc instead. Don’t forget to use :strtotime before to convert the string to a date for manipulation by :dateloc like you’d do for normal :date. You will obviously need to create the new strings for any other language and add them to your lexicon files.

    Enjoy !
      • 22303 MODX Staff
      • 10,725 Posts
      *blink* huh

      You get localized date strings by setting the locale in PHP. See setlocale().
        • 23560
        • 2 Posts
        The way my website is setup makes it hard for me to use setLocate. This way is easier to me, respects the cultureKey on my multilingual websites, etc. It’s a no fuss solution.
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          I’d probably just include it in a snippet and use that as your output modifier - keeps your upgrade path clean.
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.