为多语言网站翻译 PHP date()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4975854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:35  来源:igfitidea点击:

Translating PHP date() for Multilingual Site

phpdatedatetime

提问by Brian

I'm trying to create a conditional translation of the PHP internal date() function. Is it possible to somehow redefine the internal variables - e.g. - date('M'), date('y') etc so that different strings are fed into the remainder of the PHP function on the basis of this test:

我正在尝试创建 PHP 内部 date() 函数的条件转换。是否有可能以某种方式重新定义内部变量 - 例如 - date('M'), date('y') 等,以便根据此测试将不同的字符串输入 PHP 函数的其余部分:

if (ICL_LANGUAGE_CODE == 'fr') { }

The following is a working example of the code I'm using for a dates module. Since $dateis defined with many variables contained in this definition it's important to conditionally re-define the variables within PHP's date()first in order to avoid having to redefine the variable 100 times or more within each key.

以下是我用于日期模块的代码的工作示例。由于$date此定义中包含许多变量,因此有条件地重新定义 PHP 中的date()第一个变量很重要,以避免在每个键中重新定义变量 100 次或更多。

if($start <= $end):
    if($start == $end):
        //Month Day, Year
        $date =  date('F', $start).' '.date('j',$start).', '.date('Y', $start);
    else:
        if($start_year == $end_year):
            if($start_month == $end_month):

                //Month Day - Day, Year
                $date = date('F', $start).' '.date('j',$start).' - '.date('j', $end).', '.date('Y', $start);
            else:
                //Month Day - Month Day, Year
                $date =  date('F', $start).' '.date('j',$start).' - '.date('F', $end).' '.date('j', $end).', '.date('Y', $start);
            endif;
        else:
            //Month Day, Year - Month Day, Year
            $date =  date('F', $start).' '.date('j',$start).', '.date('Y', $start).' - '.date('F', $end).' '.date('j', $end).', '.date('Y', $end);
        endif;
    endif;
endif;

回答by netcoder

Whenever you need to manipulate date/time stamps based on locale, you should use strftime:

每当您需要根据区域设置操作日期/时间戳时,您应该使用strftime

switch ($lang) {
    case 'en':
        setlocale(LC_TIME, 'en_CA.UTF-8');
        echo strftime("%B %e, %G");
        break;
    case 'fr':
        setlocale(LC_TIME, 'fr_CA.UTF-8');
        echo strftime("%e %B %G");
        break;
}

Results:

结果:

February 11, 2011  // en
11 février 2011    // fr

Of course, you need to have the locales installed on your system. In Ubuntu per example:

当然,您需要在系统上安装语言环境。在 Ubuntu 中,每个示例:

bash-4.1$ sudo locale-gen fr_CA.UTF-8

回答by Marc B

    $date =  date('F', $start).' '.date('j',$start).', '.date('Y', $start);

That's a rather painful way to go about. The format string in date()doesn't have to be a single character. This line could be reduced to

这是一种相当痛苦的方式。格式字符串date()不必是单个字符。这条线可以减少到

$date = date('F j Y');

And given that, you could have a simple

鉴于此,你可以有一个简单的

switch($whats_my_locale) {
    case 'FR':
       $format = 'date format characters for a french date';
       break
    case 'EN' :
       $format = 'format chars for english date'
       break
    case etc....
    default:
       $format = 'default date format string here';
}

$local_date_string = date($format, $start);

and off you go.

你走吧。

回答by JakeSteam

I'm sure you have, but have you considered just using the numeric values?

我确定你有,但你有没有考虑过只使用数值?

Also, if you do use them, remember the US has months / day, opposite to the UK and others.

此外,如果您确实使用它们,请记住美国有几个月/一天,与英国和其他国家相反。

回答by Chris Baaijens

Can be done rather easily with either define or a switch.

可以通过定义或开关轻松完成。

DEFINE (PHP 7)

定义 (PHP 7)

<?php
// With Define
$untranslated_date = $date('D');
translate_date($untranslated_date);
       
function translate_date($untranslated_date)
{
  define('translated_days', array(
 'Mon' => 'Ma',
 'Tue' => 'Di',
 'Wed' => 'Wo',
 'Thu' => 'Do',
 'Fri' => 'Vr',
 'Sat' => 'Za',
 'Sun' => 'Zo')
 );
 
 echo translated_days[$untranslated_date];
}

SWITCH (or if statement)

SWITCH(或 if 语句)

// With a switch (or if)
$untranslated_date = $date('D');
translate_date($untranslated_date);

function translate_date($untranslated_date)
{
switch ($untranslated_date)
{
 case "Mon":
 $translated_date = "Ma";
 break;
  
    case "Tue":
 $translated_date = "Di";
 break;
  
 case "Wed":
 $translated_date = "Wo";
 break;
  
 case "Thu":
 $translated_date = "Do";
 break;
  
 case "Fri":
 $translated_date = "Vr";
 break;
  
 case "Sat":
 $translated_date = "Za";
 break;
  
 case "Sun":
 $translated_date = "Zo";
 break;
}
 
echo $translated_date;
}