From 1e91b86ec940dc017d888ce3ae0d8e0d24ecc4f6 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 15 May 2017 21:56:52 +0000
Subject: [PATCH] Move CSV delimiter detection to i18n

---
 include/class.export.php | 12 +-----------
 include/class.i18n.php   | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/include/class.export.php b/include/class.export.php
index 58a424628..f9613eeba 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -372,17 +372,7 @@ class CsvResultsExporter extends ResultSetExporter {
         if (!$this->output)
              $this->output = fopen('php://output', 'w');
 
-        // Detect delimeter from the current locale settings. For locales
-        // which use comma (,) as the decimal separator, the semicolon (;)
-        // should be used as the field separator
-        $delimiter = ',';
-        if (class_exists('NumberFormatter')) {
-            $nf = NumberFormatter::create(Internationalization::getCurrentLocale(),
-                NumberFormatter::DECIMAL);
-            $s = $nf->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
-            if ($s == ',')
-                $delimiter = ';';
-        }
+        $delimiter = Internationalization::getCSVDelimiter();
 
         // Output a UTF-8 BOM (byte order mark)
         fputs($this->output, chr(0xEF) . chr(0xBB) . chr(0xBF));
diff --git a/include/class.i18n.php b/include/class.i18n.php
index 69814a0a3..19e05aa70 100644
--- a/include/class.i18n.php
+++ b/include/class.i18n.php
@@ -401,6 +401,30 @@ class Internationalization {
         return $locale;
     }
 
+    static function getCSVDelimiter($locale='') {
+
+        if (!$locale)  // Prefer browser settings
+            $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
+
+        // Detect delimeter from the current locale settings. For locales
+        // which use comma (,) as the decimal separator, the semicolon (;)
+        // should be used as the field separator
+        $delimiter = ',';
+        if (class_exists('NumberFormatter')) {
+            $nf = NumberFormatter::create($locale ?: self::getCurrentLocale(),
+                NumberFormatter::DECIMAL);
+            $s = $nf->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
+            if ($s == ',')
+                $delimiter = ';';
+        } else {
+            $info = localeconv();
+            if ($info && $info['decimal_point'] == ',')
+                $delimiter = ';';
+
+        }
+
+        return $delimiter;
+    }
 
     //  getIntDateFormatter($options)
     //
-- 
GitLab