Project

General

Profile

Bug #1733 » support-localized-decimal-separator_5.0.6.patch

liaham, 10/14/2023 04:36 PM

View differences:

app/helpers/application_helper.rb
end
end
# [xmera]: Changed float to accept localized delimiter and separator for
# float values.
# Helper that formats object for html or text rendering
def format_object(object, html=true, &block)
if block_given?
......
when 'Fixnum'
object.to_s
when 'Float'
sprintf "%.2f", object
number_with_delimiter(object, delimiter: ::I18n.t('number.format.delimiter'),
separator: ::I18n.t('number.format.separator'),
trip_insignificant_zeros: false,
precision: 2)
when 'User', 'Group'
html ? link_to_principal(object) : object.to_s
when 'Project'
app/models/custom_field_value.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class CustomFieldValue
# [xmera]: Includes Redmine::I18n#normalize_float
include Redmine::I18n
attr_accessor :custom_field, :customized, :value_was
attr_reader :value
......
end
end
# [xmera]: Changed value to be normalized. That is using dot instead
# of comma as separator for float values.
def validate_value
self.value = normalize_float(value) if float_as_string?
custom_field.validate_custom_value(self).each do |message|
customized.errors.add(custom_field.name, message)
end
end
# [xmera]: Checks whether the custom field is a float format and given as
# string to avoid a normalization of string values from other custom
# field formats.
def float_as_string?
self.custom_field.field_format == 'float' && self.value.is_a?(String)
end
end
lib/redmine/field_format.rb
def validate_single_value(custom_field, value, customized=nil)
errs = super
errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
errs << ::I18n.t('activerecord.errors.messages.invalid') unless Kernel.Float(value, exception: false)
errs
end
lib/redmine/i18n.rb
end
end
# [xmera]: Will consider language specific separator in user input
# and normalize them to a unified format to be accepted by Kernel.Float().
#
# @param value [String] A string represenation of a float value.
#
# @note The delimiter cannot be used here if it is a decimal point since it
# will clash with the dot separator.
def normalize_float(value)
separator = ::I18n.t('number.format.separator')
value.gsub!(/[#{separator}]/, separator => '.')
end
def day_name(day)
::I18n.t('date.day_names')[day % 7]
end
test/unit/lib/redmine/field_format/numeric_format_test.rb
class Redmine::NumericFieldFormatTest < ActionView::TestCase
include ApplicationHelper
fixtures :projects, :users, :issue_statuses, :enumerations,
:trackers, :projects_trackers, :roles, :member_roles,
:members, :enabled_modules
def setup
User.current = nil
end
......
assert_equal 3, field.format.formatted_custom_value(self, custom_value, false)
assert_equal '<a class="external" href="http://foo/3">3</a>', field.format.formatted_custom_value(self, custom_value, true)
end
# [xmera]: Should test localized float values (separator, delimiter)
def test_float_field_value_should_validate_when_given_with_various_separator
field = IssueCustomField.generate!(field_format: 'float')
issue = Issue.generate!(tracker: Tracker.find(1), status: IssueStatus.find(1), priority: IssuePriority.find(6))
::I18n.locale = :de
assert field.format.validate_single_value(field, '3,33', issue)
::I18n.locale = :en
assert field.format.validate_single_value(field, '3.33', issue)
end
# [xmera]: Should test localized float values (separator, delimiter)
def test_float_field_should_format_with_various_locale_separator_and_delimiter
field = IssueCustomField.generate!(field_format: 'float')
issue = Issue.generate!(tracker: Tracker.find(1), status: IssueStatus.find(1), priority: IssuePriority.find(6))
::I18n.locale = :de
issue.custom_field_values = { field.id => '3333.33' }
issue.save!
assert_equal '3.333,33', format_object(issue.reload.custom_field_values.first, false)
::I18n.locale = :en
assert_equal '3333.33', format_object(issue.reload.custom_field_values.first, false)
end
end
    (1-1/1)