]> source.dussan.org Git - redmine.git/commitdiff
Support localized decimal separators for float values (#22024).
authorGo MAEDA <maeda@farend.jp>
Thu, 4 Jan 2024 06:55:14 +0000 (06:55 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 4 Jan 2024 06:55:14 +0000 (06:55 +0000)
Patch by Liane Hampe (@liane_hampe).

git-svn-id: https://svn.redmine.org/redmine/trunk@22592 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
lib/redmine/field_format.rb
lib/redmine/i18n.rb
test/unit/lib/redmine/field_format/numeric_format_test.rb

index ac7cc3c50351f94091e2072db6e711fca3d34441..dcf1da35b76a97aebe5592907946904a9e6ce7e8 100644 (file)
@@ -266,7 +266,7 @@ module ApplicationHelper
     when Integer
       object.to_s
     when Float
-      sprintf "%.2f", object
+      number_with_delimiter(sprintf('%.2f', object), delimiter: nil)
     when User, Group
       html ? link_to_principal(object) : object.to_s
     when Project
index a27311141b71caa3b752f8cbc510b82c1dab0b4a..5e81055f1389ef6a14b2c3980a080d93abaa105f 100644 (file)
@@ -539,8 +539,9 @@ module Redmine
       end
 
       def validate_single_value(custom_field, value, customized=nil)
+        value = normalize_float(value)
         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
 
index 8434cd9be72e23f6d08d3106970ea4dc453c707b..dab4868932ab477baf71c756aa8c24c037716a63 100644 (file)
@@ -99,6 +99,18 @@ module Redmine
       end
     end
 
+    # 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
index 9ced022fbbbfa7811de4965a18e62998e4fe0e42..c1914d2e8427637faedeb769c713bd0399e49e97 100644 (file)
@@ -23,6 +23,10 @@ require 'redmine/field_format'
 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
@@ -34,4 +38,28 @@ class Redmine::NumericFieldFormatTest < ActionView::TestCase
     assert_equal 3, field.format.formatted_custom_value(self, custom_value, false)
     assert_equal '<a href="http://foo/3" class="external">3</a>', field.format.formatted_custom_value(self, custom_value, true)
   end
+
+  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))
+    to_test = {'en' => '3.33', 'de' => '3,33'}
+    to_test.each do |locale, expected|
+      with_locale locale do
+        assert field.format.validate_single_value(field, expected, issue)
+      end
+    end
+  end
+
+  def test_float_field_should_format_with_various_locale_separator
+    field = IssueCustomField.generate!(field_format: 'float')
+    issue = Issue.generate!(tracker: Tracker.find(1), status: IssueStatus.find(1), priority: IssuePriority.find(6))
+    issue.custom_field_values = { field.id => '1234.56' }
+    issue.save!
+    to_test = {'en' => '1234.56', 'de' => '1234,56'}
+    to_test.each do |locale, expected|
+      with_locale locale do
+        assert_equal expected, format_object(issue.reload.custom_field_values.last, false)
+      end
+    end
+  end
 end