Sha256: a140e0297ea2ea9f310797143b5483f3c1ccb89216779ce2460155f68a9067d9

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'i18n/backend/base'

module I18n
  module Backend
    class Mongoid
      module Implementation
        attr_accessor :model
        include Base, Flatten

        def initialize(model)
          @model = model
          init_translations
        end

        def initialized?
          @initialized ||= false
        end

        # Stores translations for the given locale in memory.
        # This uses a deep merge for the translations hash, so existing
        # translations will be overwritten by new ones only at the deepest
        # level of the hash.
        def store_translations(locale, data, options = {})
          locale = locale.to_sym
          translations[locale] ||= {}
          data = data.deep_symbolize_keys
          translations[locale].deep_merge!(data)
        end

        # Get available locales from the translations hash
        def available_locales
          init_translations unless initialized?
          translations.inject([]) do |locales, (locale, data)|
            locales << locale unless (data.keys - [:i18n]).empty?
            locales
          end
        end

        # Clean up translations hash and set initialized to false on reload!
        def reload!
          @initialized = false
          @translations = nil
          init_translations
        end

        protected

          def init_translations
            load_translations
            @initialized = true
          end

          def load_translations
            @translations = {}
            @model.all.each{ |t| @lookuptranslations[t.key] = t.value_translations }
          end

          def translations
            @translations ||= {}
          end

          def lookup(locale, key, scope = [], options = {})
            init_translations unless initialized?
            #translations.dig(key,value)
             translations[key].blank? ? nil : translations[key][locale]
          end
      end

      include Implementation
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
venomi-0.0.3 lib/generators/templates/mongoid.rb
venomi-0.0.2 lib/generators/templates/mongoid.rb
venomi-0.0.1 lib/generators/templates/mongoid.rb