From b02deefb398526d3eb29a1d6c064de280f45175a Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 25 Mar 2015 15:29:10 -0500 Subject: [PATCH] i18n: Consider current locale for CSV delimiter For locales which use comma as the decimal separator, CSV exports should be generated using a semi-colon (;) rather than a comma (,) since spreadsheet programs in such locales will have difficulty with comma-delimited CSV files. --- include/class.export.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/class.export.php b/include/class.export.php index 3210d3625..447435b41 100644 --- a/include/class.export.php +++ b/include/class.export.php @@ -290,10 +290,23 @@ 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 = ';'; + } + + // Output a UTF-8 BOM (byte order mark) fputs($this->output, chr(0xEF) . chr(0xBB) . chr(0xBF)); - fputcsv($this->output, $this->getHeaders()); + fputcsv($this->output, $this->getHeaders(), $delimiter); while ($row=$this->next()) - fputcsv($this->output, $row); + fputcsv($this->output, $row, $delimiter); fclose($this->output); } -- GitLab