From 90931b0a2717ff80d674d476114d5e7be68373ed Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 19 Mar 2012 00:08:15 +0100 Subject: [PATCH] fixed compilation of numeric values used in templates when using a locale where the decimal separator is not a dot --- CHANGELOG | 1 + lib/Twig/Compiler.php | 10 ++++++++++ test/Twig/Tests/CompilerTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 0 deletions(-) create mode 100644 test/Twig/Tests/CompilerTest.php diff --git a/CHANGELOG b/CHANGELOG index da3e3fb..058e137 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.7.0 (2012-XX-XX) + * fixed compilation of numeric values used in templates when using a locale where the decimal separator is not a dot * made the strategy used to guess the real template file name and line number in exception messages much faster and more accurate * 1.6.2 (2012-03-18) diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php index 149a5a0..cd7f3ff 100644 --- a/lib/Twig/Compiler.php +++ b/lib/Twig/Compiler.php @@ -146,6 +146,16 @@ class Twig_Compiler implements Twig_CompilerInterface public function repr($value) { if (is_int($value) || is_float($value)) { + if (false !== $locale = setlocale(LC_NUMERIC, 0)) { + setlocale(LC_NUMERIC, 'C'); + } + + $value = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value)); + + if (false !== $locale) { + setlocale(LC_NUMERIC, $locale); + } + $this->raw($value); } elseif (null === $value) { $this->raw('null'); diff --git a/test/Twig/Tests/CompilerTest.php b/test/Twig/Tests/CompilerTest.php new file mode 100644 index 0000000..ebe79ae --- /dev/null +++ b/test/Twig/Tests/CompilerTest.php @@ -0,0 +1,33 @@ +markTestSkipped('Your platform does not support locales.'); + } + + $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252'); + if (false === setlocale(LC_ALL, $required_locales)) { + $this->markTestSkipped('Could not set any of required locales: ' . implode(", ", $required_locales)); + } + + $this->assertEquals('1.2', $compiler->repr(1.2)->getSource()); + $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0))); + + setlocale(LC_ALL, $locale); + } +} -- 1.7.2.5