Defect #36461

I18nTest#test_custom_pluralization_rules randomly fails

Added by Yuichi HARADA 4 months ago. Updated 4 months ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:I18n
Target version:-
Resolution:Fixed Affected version:

Description

The test will be always failed if the following conditions are met:

  • using trunk(r21372)
  • Run the test specified a seed value( TESTOPTS="--seed 46869" )
% RAILS_ENV=test bundle exec rake db:migrate:reset

% RAILS_ENV=test bundle exec rake test TESTOPTS="--seed 46869" 
(Test LDAP server not configured)
Bazaar non ASCII output test cannot run on this environment.
Encoding.locale_charmap: UTF-8
Skipping LDAP tests.
Run options: --seed 46869

# Running:

.................F

Failure:
Redmine::I18nTest#test_custom_pluralization_rules [test/unit/lib/redmine/i18n_test.rb:262]:
Expected: "one or none" 
  Actual: "more than one" 

rails test test/unit/lib/redmine/i18n_test.rb:259

36461.patch Magnifier (887 Bytes) Yuichi HARADA, 2022-01-21 06:59


Related issues

Related to Redmine - Defect #36396: Custom I18n Pluralization rules are not applied correctly Closed

Associated revisions

Revision 21382
Added by Go MAEDA 4 months ago

Fix that I18nTest#test_custom_pluralization_rules randomly fails (#36396, #36461).

Patch by Yuichi HARADA.

History

#1 Updated by Go MAEDA 4 months ago

  • Related to Defect #36396: Custom I18n Pluralization rules are not applied correctly added

#2 Updated by Yuichi HARADA 4 months ago

It seems that pluralization rules(config/locales/*.rb) is also loaded when loading the locale files located in I18n.load_path(source:/trunk/config/application.rb#L42).

https://github.com/ruby-i18n/i18n/wiki/Pluralizations

One can ship pluralizers (i.e. lambdas that implement locale specific pluralization algorithms) as part of any Ruby translation file anywhere in the I18n.load_path. The implementation expects to find them with the key :pluralize in a (newly invented) translation metadata namespace :i18n.

Therefore, it seems that you can't set pluralization rules in the middle like I18nTest#test_custom_pluralization_rules.
I have confirmed that the tests succeed with the following patch.

diff --git a/test/unit/lib/redmine/i18n_test.rb b/test/unit/lib/redmine/i18n_test.rb
index 4e15ae56b7..f6e7a804d1 100644
--- a/test/unit/lib/redmine/i18n_test.rb
+++ b/test/unit/lib/redmine/i18n_test.rb
@@ -257,11 +257,13 @@ class Redmine::I18nTest < ActiveSupport::TestCase
   end

   def test_custom_pluralization_rules
+    pluralizers = I18n.backend.instance_variable_get(:@pluralizers)
+    I18n.backend.instance_variable_set(:@pluralizers, nil)
     I18n.backend.store_translations :pt, i18n: {plural: {rule: ->(n) {[0, 1].include?(n) ? :one : :other }}}
     I18n.backend.store_translations :pt, apples: {one: 'one or none', other: 'more than one'}
     assert_equal 'one or none', ll(:pt, :apples, count: 0)
     assert_equal 'more than one', ll(:pt, :apples, count: 2)
   ensure
-    I18n.reload!
+    I18n.backend.instance_variable_set(:@pluralizers, pluralizers)
   end
 end

#3 Updated by Go MAEDA 4 months ago

  • Subject changed from I18nTest#test_custom_pluralization_rules randomly fail to I18nTest#test_custom_pluralization_rules randomly fails
  • Category set to I18n
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the fix as a part of #36396. Thank you.

#4 Updated by Go MAEDA 4 months ago

  • Status changed from New to Closed

Also available in: Atom PDF