Class: I18n::Backend::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Base, Links
Defined in:
lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record/translation.rb,
lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record.rb,
lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record/missing.rb,
lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record/store_procs.rb

Overview

ActiveRecord model used to store actual translations to the database.

This model expects a table like the following to be already set up in your the database:

create_table :translations do |t|
  t.string :locale
  t.string :key
  t.text   :value
  t.text   :interpolations
  t.boolean :is_proc, :default => false
end

This model supports to named scopes :locale and :lookup. The :locale scope simply adds a condition for a given locale:

I18n::Backend::ActiveRecord::Translation.locale(:en).all
# => all translation records that belong to the :en locale

The :lookup scope adds a condition for looking up all translations that either start with the given keys (joined by an optionally given separator or I18n.default_separator) or that exactly have this key.

# with translations present for :"foo.bar" and :"foo.baz"
I18n::Backend::ActiveRecord::Translation.lookup(:foo)
# => an array with both translation records :"foo.bar" and :"foo.baz"

I18n::Backend::ActiveRecord::Translation.lookup([:foo, :bar])
I18n::Backend::ActiveRecord::Translation.lookup(:"foo.bar")
# => an array with the translation record :"foo.bar"

When the StoreProcs module was mixed into this model then Procs will be stored to the database as Ruby code and evaluated when :value is called.

Translation = I18n::Backend::ActiveRecord::Translation
Translation.create \
  :locale => 'en'
  :key    => 'foo'
  :value  => lambda { |key, options| 'FOO' }
Translation.find_by_locale_and_key('en', 'foo').value
# => 'FOO'

Defined Under Namespace

Modules: Missing, StoreProcs Classes: Translation

Constant Summary

Constants included from Base

Base::INTERPOLATION_SYNTAX_PATTERN, Base::RESERVED_KEYS

Constants included from Helpers

Helpers::SEPARATOR_ESCAPE_CHAR

Instance Method Summary collapse

Methods included from Base

#initialized?, #load_translations, #localize, #translate

Methods included from Helpers

#deep_symbolize_keys, #escape_default_separator, #unescape_default_separator, #unwind_keys, #wind_keys

Instance Method Details

#available_localesObject



25
26
27
28
29
30
31
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record.rb', line 25

def available_locales
  begin
    Translation.available_locales
  rescue ::ActiveRecord::StatementInvalid
    []
  end
end

#reload!Object



13
14
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record.rb', line 13

def reload!
end

#store_translations(locale, data, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/active_record.rb', line 16

def store_translations(locale, data, options = {})
  separator = options[:separator] || I18n.default_separator
  wind_keys(data, separator).each do |key, value|
    store_link(locale, key, value) if value.is_a?(Symbol)
    Translation.locale(locale).lookup(expand_keys(key, separator), separator).delete_all
    Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
  end
end