From ec0ce00b95c4d08c2c7c9bae79d16f84829349fb Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Fri, 7 Jan 2022 01:29:55 +0000 Subject: 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 --- lib/redmine/codeset_util.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') 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 -- cgit v1.2.3