diff options
author | Go MAEDA <maeda@farend.jp> | 2022-01-07 01:29:55 +0000 |
---|---|---|
committer | Go MAEDA <maeda@farend.jp> | 2022-01-07 01:29:55 +0000 |
commit | ec0ce00b95c4d08c2c7c9bae79d16f84829349fb (patch) | |
tree | ef315418de950296aeb2c000f615417e375ad50e /app | |
parent | 03cd59edc05e540ff549903170e52051bcca229b (diff) | |
download | redmine-ec0ce00b95c4d08c2c7c9bae79d16f84829349fb.tar.gz redmine-ec0ce00b95c4d08c2c7c9bae79d16f84829349fb.zip |
Auto guess file encoding when importing CSV file (#34718).
Patch by Go MAEDA.
git-svn-id: http://svn.redmine.org/redmine/trunk@21352 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/models/import.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index 92752a3db..ffa69d22e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -65,15 +65,23 @@ class Import < ActiveRecord::Base def set_default_settings(options={}) separator = lu(user, :general_csv_separator) + encoding = lu(user, :general_csv_encoding) if file_exists? begin content = File.read(filepath, 256) + separator = [',', ';'].sort_by {|sep| content.count(sep)}.last + + guessed_encoding = Redmine::CodesetUtil.guess_encoding(file_content) + encoding = + (guessed_encoding && ( + Setting::ENCODINGS.detect {|e| e.casecmp?(guessed_encoding)} || + Setting::ENCODINGS.detect {|e| Encoding.find(e) == Encoding.find(guessed_encoding)} + )) || lu(user, :general_csv_encoding) rescue => e end end wrapper = '"' - encoding = lu(user, :general_csv_encoding) date_format = lu(user, "date.formats.default", :default => "foo") date_format = DATE_FORMATS.first unless DATE_FORMATS.include?(date_format) |