Sha256: 1056e0e9d7a4c3351668909714a5a0f79c7913f01ff6bef70c7d367198e32b16

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module WCC::Contentful::Middleware::Store
  ##
  # This middleware enforces that all entries returned by the store layer are properly localized.
  # It does this by transforming entries from the store's "locale=*" format into the specified locale (or default).
  #
  # Stores keep entries in the "locale=*" format, which is a hash of all locales for each field.  This is convenient
  # because the Sync API returns them in this format.  However, the Model layer requires localized entries.  So, to
  # separate concerns, this middleware handles the transformation.
  class LocaleMiddleware
    include WCC::Contentful::Middleware::Store
    include WCC::Contentful::EntryLocaleTransformer

    attr_accessor :configuration

    def default_locale
      @default_locale ||= configuration&.default_locale&.to_s || 'en-US'
    end

    def transform(entry, options)
      locale = options[:locale]&.to_s || default_locale
      if locale == '*'
        transform_to_star(entry)
      else
        transform_to_locale(entry, locale)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wcc-contentful-1.7.2 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.7.1 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.7.0 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.6.2 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.6.1 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.6.0 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.5.1 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.5.0 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.5.0.rc1 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.4.0 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.4.0.rc3 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.4.0.rc2 lib/wcc/contentful/middleware/store/locale_middleware.rb
wcc-contentful-1.4.0.rc1 lib/wcc/contentful/middleware/store/locale_middleware.rb