Zend 国际化视图助手
在我当前的项目中,我需要使用一个助手,并且我想在 .po 文件中翻译此视图助手内的字符串。所以,我认为我需要使用 translate() 助手,但我不知道如何在助手中使用它。
这是我在网上找到的帮助程序:
<?php
/**
*
* @category View_Helper
* @package Custom_View_Helper
* @author Chris Jones <[email protected]>
* @license New BSD License
*/
class Zend_View_Helper_HumaneDate extends Zend_View_Helper_Abstract {
/**
* Various time formats
*/
private static $_time_formats = array(
array(60, 'un instant'),
array(90, '1 minute'), // 60*1.5
array(3600, 'minutes', 60), // 60*60, 60
array(5400, '1 heure'), // 60*60*1.5
array(86400, 'heures', 3600), // 60*60*24, 60*60
array(129600, '1 jour'), // 60*60*24*1.5
array(604800, 'jours', 86400), // 60*60*24*7, 60*60*24
array(907200, '1 semaine'), // 60*60*24*7*1.5
array(2628000, 'semaines', 604800), // 60*60*24*(365/12), 60*60*24*7
array(3942000, '1 mois'), // 60*60*24*(365/12)*1.5
array(31536000, 'mois', 2628000), // 60*60*24*365, 60*60*24*(365/12)
array(47304000, '1 année'), // 60*60*24*365*1.5
array(3153600000, 'années', 31536000), // 60*60*24*365*100, 60*60*24*365
);
/**
* Convert date into a pretty 'human' form
* Now with microformats!
*
* @param string $ |Zend_Date $date_from Date to convert
* @return string
*/
public function humaneDate($date_from)
{
$date_to = new Zend_Date(null, Zend_Date::ISO_8601);
if (!($date_from instanceof Zend_Date)) {
$date_from = new Zend_Date($date_from, Zend_Date::ISO_8601);
}
$dateTo = $date_to->getTimestamp(); // UnixTimestamp
$dateFrom = $date_from->getTimestamp(); // UnixTimestamp
$difference = $dateTo - $dateFrom;
$message = '';
if ($dateFrom <= 0) {
$message = 'il y a longtemps';
} else {
foreach (self::$_time_formats as $format) {
if ($difference < $format[0]) {
if (count($format) == 2) {
$message = 'il y a ' . $format[1];
break;
} else {
$message = 'il y a ' . ceil($difference / $format[2]) . ' ' . $format[1];
break;
}
}
}
}
return sprintf('<abbr title="%sZ">%s</abbr>',
$date_from->get('YYYY-MM-ddTHH:mm:ss'),
$message
);
}
}
有什么想法吗?
in my current project, I need to to use a helper and I would like to translate the string inside this view helper in my .po files. So, I think i need to use the translate() helper but I don't figure out how to use it in a helper.
Here is the helper I found on the web :
<?php
/**
*
* @category View_Helper
* @package Custom_View_Helper
* @author Chris Jones <[email protected]>
* @license New BSD License
*/
class Zend_View_Helper_HumaneDate extends Zend_View_Helper_Abstract {
/**
* Various time formats
*/
private static $_time_formats = array(
array(60, 'un instant'),
array(90, '1 minute'), // 60*1.5
array(3600, 'minutes', 60), // 60*60, 60
array(5400, '1 heure'), // 60*60*1.5
array(86400, 'heures', 3600), // 60*60*24, 60*60
array(129600, '1 jour'), // 60*60*24*1.5
array(604800, 'jours', 86400), // 60*60*24*7, 60*60*24
array(907200, '1 semaine'), // 60*60*24*7*1.5
array(2628000, 'semaines', 604800), // 60*60*24*(365/12), 60*60*24*7
array(3942000, '1 mois'), // 60*60*24*(365/12)*1.5
array(31536000, 'mois', 2628000), // 60*60*24*365, 60*60*24*(365/12)
array(47304000, '1 année'), // 60*60*24*365*1.5
array(3153600000, 'années', 31536000), // 60*60*24*365*100, 60*60*24*365
);
/**
* Convert date into a pretty 'human' form
* Now with microformats!
*
* @param string $ |Zend_Date $date_from Date to convert
* @return string
*/
public function humaneDate($date_from)
{
$date_to = new Zend_Date(null, Zend_Date::ISO_8601);
if (!($date_from instanceof Zend_Date)) {
$date_from = new Zend_Date($date_from, Zend_Date::ISO_8601);
}
$dateTo = $date_to->getTimestamp(); // UnixTimestamp
$dateFrom = $date_from->getTimestamp(); // UnixTimestamp
$difference = $dateTo - $dateFrom;
$message = '';
if ($dateFrom <= 0) {
$message = 'il y a longtemps';
} else {
foreach (self::$_time_formats as $format) {
if ($difference < $format[0]) {
if (count($format) == 2) {
$message = 'il y a ' . $format[1];
break;
} else {
$message = 'il y a ' . ceil($difference / $format[2]) . ' ' . $format[1];
break;
}
}
}
}
return sprintf('<abbr title="%sZ">%s</abbr>',
$date_from->get('YYYY-MM-ddTHH:mm:ss'),
$message
);
}
}
Any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你的助手扩展 Zend_View_Helper 时,你可以使用助手的
$view
memeber 变量访问视图中的任何内容。使用这个语法:As your helper extends Zend_View_Helper you can access anything like in view using the
$view
memeber variable of the helper. Use this syntax: