# HG changeset patch # Parent 6b1e016513ea6189920c55f1f3e308091ec6ec76 # User Toshi MARUYAMA [mq]: 7037-csv-bom.diff diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -162,7 +162,7 @@ module QueriesHelper end def query_to_csv(items, query, options={}) - encoding = l(:general_csv_encoding) + encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding) columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) query.available_block_columns.each do |column| if options[column.name].present? @@ -178,6 +178,7 @@ module QueriesHelper csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) } end end + export = "\xEF\xBB\xBF".force_encoding("UTF-8") + export if Setting.csv_utf8_bom? export end diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -90,6 +90,7 @@ module TimelogHelper def report_to_csv(report) decimal_separator = l(:general_csv_decimal_separator) + encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding) export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| # Column headers headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } @@ -97,11 +98,11 @@ module TimelogHelper headers << l(:label_total_time) csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( c.to_s, - l(:general_csv_encoding) ) } + encoding)} # Content report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) # Total row - str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding)) + str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), encoding) row = [ str_total ] + [''] * (report.criteria.size - 1) total = 0 report.periods.each do |period| @@ -112,18 +113,20 @@ module TimelogHelper row << ("%.2f" % total).gsub('.',decimal_separator) csv << row end + export = "\xEF\xBB\xBF".force_encoding("UTF-8") + export if Setting.csv_utf8_bom? export end def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) decimal_separator = l(:general_csv_decimal_separator) + encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding) hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| hours_for_value = select_hours(hours, criteria[level], value) next if hours_for_value.empty? row = [''] * level row << Redmine::CodesetUtil.from_utf8( format_criteria_value(available_criteria[criteria[level]], value).to_s, - l(:general_csv_encoding) ) + encoding) row += [''] * (criteria.length - level - 1) total = 0 periods.each do |period| @@ -137,5 +140,6 @@ module TimelogHelper report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) end end + csv = "\xEF\xBB\xBF".force_encoding("UTF-8") + csv if Setting.csv_utf8_bom? end end diff --git a/app/views/settings/_general.html.erb b/app/views/settings/_general.html.erb --- a/app/views/settings/_general.html.erb +++ b/app/views/settings/_general.html.erb @@ -35,6 +35,8 @@

<%= setting_text_field :repositories_encodings, :size => 60 %> <%= l(:text_comma_separated) %>

+

<%= setting_check_box :csv_utf8_bom %>

+ <%= call_hook(:view_settings_general_form) %> diff --git a/config/locales/en.yml b/config/locales/en.yml --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -416,6 +416,7 @@ en: setting_link_copied_issue: Link issues on copy setting_max_additional_emails: Maximum number of additional email addresses setting_search_results_per_page: Search results per page + setting_csv_utf8_bom: CSV in UTF-8 BOM (Byte Order Mark) permission_add_project: Create project permission_add_subprojects: Create subprojects diff --git a/config/settings.yml b/config/settings.yml --- a/config/settings.yml +++ b/config/settings.yml @@ -240,3 +240,5 @@ non_working_week_days: default: - '6' - '7' +csv_utf8_bom: + default: 1