Skip to content

Commit

Permalink
Merge pull request #38659 from stevecrozz/inflection-locale-defaults-…
Browse files Browse the repository at this point in the history
…and-fallbacks

Inflection support default_locale and fallbacks
  • Loading branch information
rafaelfranca committed Dec 9, 2020
2 parents 297a6b9 + ea27ff3 commit d3ccc92
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions activesupport/lib/active_support/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "active_support/core_ext/hash/slice"
begin
require "i18n"
require "i18n/backend/fallbacks"
rescue LoadError => e
$stderr.puts "The i18n gem is not available. Please add it to your Gemfile and run bundle install"
raise e
Expand Down
9 changes: 8 additions & 1 deletion activesupport/lib/active_support/inflector/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def self.instance(locale = :en)
@__instance__[locale] ||= new
end

def self.instance_or_fallback(locale)
I18n.fallbacks[locale].each do |k|
return @__instance__[k] if @__instance__.key?(k)
end
instance(locale)
end

attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms

attr_reader :acronyms_camelize_regex, :acronyms_underscore_regex # :nodoc:
Expand Down Expand Up @@ -248,7 +255,7 @@ def inflections(locale = :en)
if block_given?
yield Inflections.instance(locale)
else
Inflections.instance(locale)
Inflections.instance_or_fallback(locale)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/inflector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def test_pluralize_empty_string
assert_equal "", ActiveSupport::Inflector.pluralize("")
end

def test_pluralize_with_fallback
I18n.stub(:default_locale, :"en-GB") do
assert_equal "days", ActiveSupport::Inflector.pluralize("days")
end
end

test "uncountability of ascii word" do
word = "HTTP"
ActiveSupport::Inflector.inflections do |inflect|
Expand Down

0 comments on commit d3ccc92

Please sign in to comment.