From 8126c8e1b0fc224cf315341c0f0776fa83f2f61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Le=CC=81on?= Date: Wed, 11 Jun 2014 17:53:19 +0200 Subject: [PATCH 1/4] Allow localization of date formating --- .../Thelia/Core/Template/Smarty/Plugins/Format.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php index 372f74d3f..4744b5bc6 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -50,6 +50,7 @@ class Format extends AbstractSmartyPlugin * * ex : * {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format + * {format_date date=$dateTimeObject format="% %B %Y" locale="fr_FR"} will output the format with specific format * {format_date date=$dateTimeObject output="date"} will output the date using the default date system format * {format_date date=$dateTimeObject} will output with the default datetime system format * @@ -86,7 +87,15 @@ class Format extends AbstractSmartyPlugin $format = DateTimeFormat::getInstance($this->request)->getFormat($this->getParam($params, "output", null)); } - return $date->format($format); + $locale = $this->getParam($params,'locale', false); + + if($locale === false) + { + return $date->format($format); + } else { + setlocale('LC_TIME', $locale); + return strftime($format, $date->getTimestamp()); + } } /** @@ -171,4 +180,4 @@ class Format extends AbstractSmartyPlugin new SmartyPluginDescriptor("function", "format_money", $this, "formatMoney") ); } -} +} \ No newline at end of file From f33c50fdd3975d590530f811f08a550c05bf2c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Le=CC=81on?= Date: Wed, 11 Jun 2014 17:55:14 +0200 Subject: [PATCH 2/4] Typo fix --- core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php index 4744b5bc6..87a3021a9 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -50,7 +50,7 @@ class Format extends AbstractSmartyPlugin * * ex : * {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format - * {format_date date=$dateTimeObject format="% %B %Y" locale="fr_FR"} will output the format with specific format + * {format_date date=$dateTimeObject format="%e %B %Y" locale="fr_FR"} will output the format with specific format (see strftime function) * {format_date date=$dateTimeObject output="date"} will output the date using the default date system format * {format_date date=$dateTimeObject} will output with the default datetime system format * From 7c653d63a69a91c0923e9b90e31f49bd0608e47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Le=CC=81on?= Date: Fri, 13 Jun 2014 09:54:38 +0200 Subject: [PATCH 3/4] Save and restore the current locale setting --- .../Core/Template/Smarty/Plugins/Format.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php index 87a3021a9..606479e24 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -50,7 +50,7 @@ class Format extends AbstractSmartyPlugin * * ex : * {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format - * {format_date date=$dateTimeObject format="%e %B %Y" locale="fr_FR"} will output the format with specific format (see strftime function) + * {format_date date=$dateTimeObject format="%e %B %Y" locale="fr_FR"} will output the format with specific format (see strftime() function) * {format_date date=$dateTimeObject output="date"} will output the date using the default date system format * {format_date date=$dateTimeObject} will output with the default datetime system format * @@ -93,8 +93,20 @@ class Format extends AbstractSmartyPlugin { return $date->format($format); } else { - setlocale('LC_TIME', $locale); - return strftime($format, $date->getTimestamp()); + if(function_exists('setlocale')) + { + // Save the current locale + $system_locale = setlocale('LC_TIME', 0); + setlocale('LC_TIME', $locale); + $localized_date = strftime($format, $date->getTimestamp()); + // Restore the locale + setlocale('LC_TIME', $system_locale); + + return $localized_date; + } else { + // No timestamp => error + throw new SmartyPluginException("The setlocale() function is not available on your system."); + } } } From 20593a05761c76aa21b0bbd6d90ec81687d2ee68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Le=CC=81on?= Date: Sat, 14 Jun 2014 21:58:33 +0200 Subject: [PATCH 4/4] Fix PSR coding style --- core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php index 606479e24..e254ecc60 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -89,12 +89,10 @@ class Format extends AbstractSmartyPlugin $locale = $this->getParam($params,'locale', false); - if($locale === false) - { + if($locale === false) { return $date->format($format); } else { - if(function_exists('setlocale')) - { + if(function_exists('setlocale')) { // Save the current locale $system_locale = setlocale('LC_TIME', 0); setlocale('LC_TIME', $locale); @@ -104,7 +102,7 @@ class Format extends AbstractSmartyPlugin return $localized_date; } else { - // No timestamp => error + // setlocale() function not available => error throw new SmartyPluginException("The setlocale() function is not available on your system."); } }