Class: Perfectline::T9n::Backend::ActiveRecord

Inherits:
I18n::Backend::Simple
  • Object
show all
Defined in:
lib/t9n/backend/active_record.rb

Constant Summary collapse

INTERPOLATION_RESERVED_KEYS =
%w(scope default object)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActiveRecord

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_storageObject

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_backendObject

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_localesObject



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

Returns:

  • (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

Raises:

  • (MethodNotAllowed)


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, options = {})
  if locale.nil?
    raise InvalidLocale.new(locale)
  end

  if key.is_a?(Array)
    return key.map{ |k| translate(locale, k, options) }
  end

  count   = options[:count]
  scope   = options[:scope]
  object  = options[:object]
  default = options.delete(:default)
  values  = options.reject{ |name, value| INTERPOLATION_RESERVED_KEYS.include?(name.to_s) }
  entry   = lookup(locale, key, scope, object)

  if entry.nil?
    entry = default(locale, default, key, options) unless default === false

    if entry.nil?
      raise I18n::MissingTranslationData.new(locale, key, options)
    end
  end

  entry = pluralize(locale, entry, count)
  entry = interpolate(locale, entry, values)
  entry
end