Skip to content

Commit

Permalink
Fall through to I18n.fallbacks when defined
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Mar 8, 2018
1 parent 832aee7 commit a4d0ee9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ env:
- DB=mysql ORM=sequel SEQUEL_VERSION=5
- DB=sqlite3 ORM=sequel SEQUEL_VERSION=5
- DB=sqlite3 ORM= TEST_PERFORMANCE=true
- DB=sqlite3 ORM= I18N_FALLBACKS=true
matrix:
fast_finish: true
allow_failures:
Expand Down
1 change: 1 addition & 0 deletions lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Mobility
require "mobility/backends"
require "mobility/backend_resetter"
require "mobility/configuration"
require "mobility/fallbacks"
require "mobility/loaded"
require "mobility/plugins"
require "mobility/translates"
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def default_accessor_locales
def initialize
@accessor_method = :translates
@query_method = :i18n
@fallbacks_generator = lambda { |fallbacks| I18n::Locale::Fallbacks.new(fallbacks) }
@fallbacks_generator = lambda { |fallbacks| Mobility::Fallbacks.build(fallbacks) }
@default_accessor_locales = lambda { I18n.available_locales }
@default_options = Options[{
cache: true,
Expand Down
28 changes: 28 additions & 0 deletions lib/mobility/fallbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Mobility
=begin
Subclasses +I18n::Locale::Fallbacks+ such that instances of this class
fall through to fallbacks defined in +I18n.fallbacks+. This allows models to
customize fallbacks while still falling through to any fallbacks defined
globally.
=end
class Fallbacks < ::I18n::Locale::Fallbacks
# @param [Symbol] locale
# @return [Array] locales
def [](locale)
super | I18n.fallbacks[locale]
end

# For this set of fallbacks, return a new fallbacks hash.
# @param [Hash] fallbacks
# @return [I18n::Locale::Fallbacks,Mobility::Fallbacks] fallbacks hash
def self.build(fallbacks)
if I18n.respond_to?(:fallbacks)
new(fallbacks)
else
I18n::Locale::Fallbacks.new(fallbacks)
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
require 'mobility'
require "mobility/backends/null"

require "i18n/backend/fallbacks" if ENV['I18N_FALLBACKS']

I18n.enforce_available_locales = true
I18n.available_locales = [:en, :'en-US', :ja, :fr, :de, :'de-DE', :cz, :pl, :pt, :'pt-BR']

Expand Down

0 comments on commit a4d0ee9

Please sign in to comment.