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 /lib | |
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 'lib')
-rw-r--r-- | lib/redmine/codeset_util.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb index 8261e572b..875689de2 100644 --- a/lib/redmine/codeset_util.rb +++ b/lib/redmine/codeset_util.rb @@ -75,5 +75,23 @@ module Redmine str = self.replace_invalid_utf8(str) end end + + def self.guess_encoding(str) + return if str.nil? + + str = str.dup + encodings = Setting.repositories_encodings.split(',').collect(&:strip) + encodings = encodings.presence || ['UTF-8'] + + encodings.each do |encoding| + begin + str.force_encoding(encoding) + rescue Encoding::ConverterNotFoundError + # ignore if the encoding name is invalid + end + return encoding if str.valid_encoding? + end + nil + end end end |