Browse code

fixed compilation of numeric values used in templates when using a locale where the decimal separator is not a dot

Fabien Potencier authored on 18/03/2012 23:08:15
Showing 3 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.7.0 (2012-XX-XX)
2 2
 
3
+ * fixed compilation of numeric values used in templates when using a locale where the decimal separator is not a dot
3 4
  * made the strategy used to guess the real template file name and line number in exception messages much faster and more accurate
4 5
 
5 6
 * 1.6.2 (2012-03-18)
... ...
@@ -146,6 +146,16 @@ class Twig_Compiler implements Twig_CompilerInterface
146 146
     public function repr($value)
147 147
     {
148 148
         if (is_int($value) || is_float($value)) {
149
+            if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
150
+                setlocale(LC_NUMERIC, 'C');
151
+            }
152
+
153
+            $value = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
154
+
155
+            if (false !== $locale) {
156
+                setlocale(LC_NUMERIC, $locale);
157
+            }
158
+
149 159
             $this->raw($value);
150 160
         } elseif (null === $value) {
151 161
             $this->raw('null');
152 162
new file mode 100644
... ...
@@ -0,0 +1,33 @@
1
+<?php
2
+
3
+/*
4
+ * This file is part of Twig.
5
+ *
6
+ * (c) Fabien Potencier
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+class Twig_Tests_CompilerTest extends PHPUnit_Framework_TestCase
13
+{
14
+    public function testReprNumericValueWithLocale()
15
+    {
16
+        $compiler = new Twig_Compiler(new Twig_Environment());
17
+
18
+        $locale = setlocale(LC_NUMERIC, 0);
19
+        if (false === $locale) {
20
+            $this->markTestSkipped('Your platform does not support locales.');
21
+        }
22
+
23
+        $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
24
+        if (false === setlocale(LC_ALL, $required_locales)) {
25
+            $this->markTestSkipped('Could not set any of required locales: ' . implode(", ", $required_locales));
26
+        }
27
+
28
+        $this->assertEquals('1.2', $compiler->repr(1.2)->getSource());
29
+        $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
30
+
31
+        setlocale(LC_ALL, $locale);
32
+    }
33
+}