Module: Mobility::Backend::Fallbacks

Defined in:
lib/mobility/backend/fallbacks.rb

Overview

Falls back to one or more alternative locales in case no value is defined for a given locale.

For fallbacks: true, Mobility will use the value of Configuration#default_fallbacks for the fallbacks instance. This defaults to an instance of I18n::Locale::Fallbacks, but can be configured (see Configuration).

If a hash is passed to the fallbacks option, a new fallbacks instance will be created for the model with the hash defining additional fallbacks.

In addition, fallbacks can be disabled when reading by passing ‘fallbacks: false` to the reader method. This can be useful to determine the actual value of the translated attribute, including a possible nil value.

Examples:

With default fallbacks enabled (falls through to default locale)

class Post
  translates :title, fallbacks: true
end

I18n.default_locale = :en
Mobility.locale = :en
post = Post.new(title: "foo")

Mobility.locale = :ja
post.title
#=> "foo"

post.title = "bar"
post.title
#=> "bar"

With additional fallbacks enabled

class Post
  translates :title, fallbacks: { :'en-US' => 'de-DE', :pt => 'de-DE' }
end

Mobility.locale = :'de-DE'
post = Post.new(title: "foo")

Mobility.locale = :'en-US'
post.title
#=> "foo"

post.title = "bar"
post.title
#=> "bar"

Disabling fallbacks when reading value

class Post
  translates :title, fallbacks: true
end

I18n.default_locale = :en
Mobility.locale = :en
post = Post.new(title: "foo")

Mobility.locale = :ja
post.title
#=> "foo"
post.title(fallbacks: false)
#=> nil

See Also:

Backend Accessors collapse

Instance Method Details

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Options Hash (**options):

  • fallbacks (Boolean)

    false to disable fallbacks on lookup

Returns:

  • (Object)

    Value of translation



74
75
76
77
78
79
80
# File 'lib/mobility/backend/fallbacks.rb', line 74

def read(locale, **options)
  return super if options[:fallbacks] == false
  fallbacks[locale].detect do |locale|
    value = super(locale)
    break value if value.present?
  end
end