Sha256: 8731a2f1b3ba522cefe530710e7772f9a80adc7a77550b793315e5c071290bd9

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'i18n/backend/base'

module I18n
  module Backend
    class RemoteBackend
      module Implementation
        include Base

        attr_accessor :store

        def initialize
          @store = {}
          I18n.enforce_available_locales = false
        end

        def initialized?
          !@store.nil?
        end

        def store_translations(locale, data, _options = {})
          @store[locale] = data
        end

        def available_locales
          @store.keys.map(&:to_sym)
        end

        def translations
          @store
        end

        def init_translations
          @store
        end

        protected

        def lookup(locale, key, scope = [], options = {})
          return unless fetch_remote_translations(locale)

          keys = I18n.normalize_keys(locale, key, scope, options[:separator])
          keys.inject(translations) do |result, k|
            return nil unless result.is_a?(Hash)

            unless result.key?(k)
              k = k.to_s.to_sym
              return nil unless result.key?(k)
            end
            result[k]
          end
        end

        def fetch_remote_translations(locale)
          return @store[locale] if @store[locale]

          data = Loader.new(locale).fetch_locale
          @store[locale] = data[locale] if data

          @store[locale]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-remote_backend-0.1.0 lib/i18n/backend/remote_backend/implementation.rb