diff --git a/.travis.yml b/.travis.yml index 56524f1a5..8516ac373 100755 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,8 @@ env: - DB_USER=root before_script: + - sudo apt-get update -qq + - sudo apt-get install -y language-pack-fr language-pack-fr-base - phpenv config-add travis.php.ini - composer self-update - composer install --prefer-dist --dev diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php index e254ecc60..26cd8b1d8 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -89,22 +89,29 @@ class Format extends AbstractSmartyPlugin $locale = $this->getParam($params,'locale', false); - if($locale === false) { - return $date->format($format); + if (false === $locale) { + $value = $date->format($format); } else { - 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); + $value = $this->formatDateWithLocale($date, $locale, $format); + } - return $localized_date; - } else { - // setlocale() function not available => error - throw new SmartyPluginException("The setlocale() function is not available on your system."); - } + return $value; + } + + private function formatDateWithLocale(\DateTime $date, $locale, $format) + { + if (function_exists('setlocale')) { + // Save the current locale + $systemLocale = setlocale('LC_TIME', 0); + setlocale('LC_TIME', $locale); + $localizedDate = strftime($format, $date->getTimestamp()); + // Restore the locale + setlocale('LC_TIME', $systemLocale); + + return $localizedDate; + } else { + // setlocale() function not available => error + throw new SmartyPluginException("The setlocale() function is not available on your system."); } } @@ -190,4 +197,4 @@ class Format extends AbstractSmartyPlugin new SmartyPluginDescriptor("function", "format_money", $this, "formatMoney") ); } -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php b/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php index ad78dc670..b1643d7f9 100644 --- a/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php @@ -158,6 +158,23 @@ class FormatTest extends \PHPUnit_Framework_TestCase $this->assertEmpty($render); } + public function testFormatDateWithLocale() + { + $dateTime = new \DateTime(); + // 2014-06-17 + $dateTime->setTimestamp(1402987842); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array( + 'date' => $dateTime, + 'locale' => ['fr_FR.UTF-8', 'fr_FR'], + 'format' => '%e %B %Y' + )); + + $this->assertEquals('17 juin 2014', $render); + } + /** * test formatNumber without mandatory parameters *