翻译PHP日期()多语言网站 [英] Translating PHP date() for Multilingual Site
问题描述
我正在尝试创建PHP内部date()函数的条件转换。有可能以某种方式重新定义内部变量 - 例如 - 日期('M'),日期('y')等,以便根据此测试将不同的字符串馈送到PHP函数的其余部分:
if(ICL_LANGUAGE_CODE =='fr'){}
以下是我用于日期模块的代码的工作示例。由于 $ date
定义在此定义中包含许多变量,重要的是有条件地重新定义PHP的 date()
首先是为了避免在每个键内重新定义变量100次以上。 $($ start $ = $ end)
if($ start == $ end):
//月日,年
$ date = date('F',$ start)''.date('j',$ start)','.date('Y',$开始);
else:
if($ start_year == $ end_year):
if($ start_month == $ end_month):
//月日 - 年,年
$ date = date('F',$ start)''.date('j',$ start)' - '.date('j',$ end)','.date 'Y',$ start);
else:
//月日 - 月日,年
$ date = date('F',$ start)''.date('j',$ start) - '.date('F',$ end)''.date('j',$ end)','.date('Y',$ start);
endif;
else:
//月日,年 - 月日,年
$ date = date('F',$ start)''.date('j',$ start) '''.date('Y',$ start)' - '.date('F',$ end)''.date('j',$ end)','.date('Y ',$ end);
endif;
endif;
endif;
每当你需要操纵日期/时间戳您应该使用 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;
}
结果:
2011年2月11日// en
11février2011 // fr
当然,您需要在系统上安装区域设置。在Ubuntu的例子中:
bash-4.1 $ sudo locale-gen fr_CA.UTF-8
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:
if (ICL_LANGUAGE_CODE == 'fr') { }
The following is a working example of the code I'm using for a dates module. Since $date
is 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.
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;
Whenever you need to manipulate date/time stamps based on locale, you should use 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:
bash-4.1$ sudo locale-gen fr_CA.UTF-8
这篇关于翻译PHP日期()多语言网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!