Class: Perfectline::T9n::Backend::ActiveRecord
- Inherits:
-
I18n::Backend::Simple
- Object
- I18n::Backend::Simple
- Perfectline::T9n::Backend::ActiveRecord
- Defined in:
- lib/t9n/backend/active_record.rb
Constant Summary collapse
- INTERPOLATION_RESERVED_KEYS =
%w(scope default object)
Instance Attribute Summary collapse
-
#cache_storage ⇒ Object
Returns the value of attribute cache_storage.
-
#default_backend ⇒ Object
Returns the value of attribute default_backend.
Instance Method Summary collapse
- #available_locales ⇒ Object
- #cache_enabled? ⇒ Boolean
-
#initialize ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #reload! ⇒ Object
- #store_translation(*args) ⇒ Object
- #translate(locale, key, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
19 20 21 22 |
# File 'lib/t9n/backend/active_record.rb', line 19 def initialize @default_backend = I18n::Backend::Simple.new @cache_storage = ActiveSupport::Cache.lookup_store(:mem_cache_store, {:namespace => "i18n"}) end |
Instance Attribute Details
#cache_storage ⇒ Object
Returns the value of attribute cache_storage.
9 10 11 |
# File 'lib/t9n/backend/active_record.rb', line 9 def cache_storage @cache_storage end |
#default_backend ⇒ Object
Returns the value of attribute default_backend.
8 9 10 |
# File 'lib/t9n/backend/active_record.rb', line 8 def default_backend @default_backend end |
Instance Method Details
#available_locales ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/t9n/backend/active_record.rb', line 28 def available_locales begin ::Translation.available_locales.map(&:to_sym) rescue @default_backend.available_locales end end |
#cache_enabled? ⇒ Boolean
11 12 13 |
# File 'lib/t9n/backend/active_record.rb', line 11 def cache_enabled? return !@cache_storage.nil? end |
#reload! ⇒ Object
24 25 26 |
# File 'lib/t9n/backend/active_record.rb', line 24 def reload! # don't do anything as passenger with multiple rails instances will screw up memcached end |
#store_translation(*args) ⇒ Object
36 37 38 |
# File 'lib/t9n/backend/active_record.rb', line 36 def store_translation(*args) raise MethodNotAllowed, "Create translation not allowed for Perfectline::I18n::Backend" end |
#translate(locale, key, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/t9n/backend/active_record.rb', line 40 def translate(locale, key, = {}) if locale.nil? raise InvalidLocale.new(locale) end if key.is_a?(Array) return key.map{ |k| translate(locale, k, ) } end count = [:count] scope = [:scope] object = [:object] default = .delete(:default) values = .reject{ |name, value| INTERPOLATION_RESERVED_KEYS.include?(name.to_s) } entry = lookup(locale, key, scope, object) if entry.nil? entry = default(locale, default, key, ) unless default === false if entry.nil? raise I18n::MissingTranslationData.new(locale, key, ) end end entry = pluralize(locale, entry, count) entry = interpolate(locale, entry, values) entry end |