3

我在一个网站中使用 PHP5 类“DateTime”,现在我需要将网站从英语翻译成西班牙语,但我不知道这个类是否可以显示西班牙语中的月份和日期,如二月 - > 二月和星期一 - > 月牙。感谢您的帮助和评论!

4

1 回答 1

0

这篇文章是一个答案,因为我无法发表评论。

你调查过 stftime吗?

我个人没有很好的结果,所以我不得不恢复手动翻译月份和日期。下面的解决方案:(yii::t() 部分是翻译完成的地方)

function tDate($format,$date='now'){
    // make the date a DateTime Object 
    if(is_string($date)) $date = new DateTime($date);
    if(is_int($date)) $date = new DateTime(date('r',$date));
    if(!is_object($date)) return 'invalid input date';

    $monthIndex = $date->format("n"); // used as array index
    $dayIndex = $date->format("w"); // used as array index

    // we store in variables the translated format text 
    eval('$M = '.yii::t('app','array ( 1 => "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")').';');
    eval('$F = '.yii::t('app','array ( 1 => "January", "February", "March", "April","May", "June", "July", "August", "September", "October", "November",    "December")').';');
    eval('$D = '.yii::t('app','array (0 => "Mon", "Tue", "Wed", "Thu","Fri", "Sat", "Sun")').';');
    eval('$l = '.yii::t('app','array (0 => "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday")').';');

    $format_len = strlen($format);
    $buffer = '';

    // we perform the formatting as in date(), but we use our special cases. 
    for ($i = 0; $i < $format_len; $i++){
        switch($format[$i]){
            case 'M': $buffer .= $M[$monthIndex]; break; 
            case 'F': $buffer .= $F[$monthIndex]; break;
            case 'D': $buffer .= $D[$dayIndex]; break;
            case 'l': $buffer .= $l[$dayIndex]; break;
            case '\\': $buffer .= $format[++$i]; break;
            default: $buffer.= $date->format($format[$i]);
        }
    }
    return $buffer;
 }

基本上,它是 phpdate()函数,但重载了 M、F、D 和 l 格式(我没有费心介绍所有这些格式)。

于 2013-07-31T18:18:10.420 回答