Sha256: 79aeba830e70f513dfe04dd99041a06e64012e84949a130a62f59abbe7c9b930

Contents?: true

Size: 1.75 KB

Versions: 36

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Satis
  module Concerns
    module ContextualTranslations
      extend ActiveSupport::Concern

      included do
        #
        # This provides us with a translation helper which scopes into the original view
        # and thereby conveniently scopes the translations.
        #
        # In your component.html.slim you can use:
        # ```slim
        # = ct(".#{tab.name}", scope: [group.to_sym])
        # ````
        #
        # It'll then try and find a translation with scope: en.admin.spaces.edit.tabs.main.admin_versions
        #
        # NOTE: Add your own original_view_context method to refer to the original view context,
        #       for components this is @template
        #
        def ct(key = nil, **options)
          key = "#{full_i18n_scope(options).join('.')}#{key}" if key.start_with?('.')

          original_view_context.t(key, **options)
        end

        def full_i18n_scope(options = {})
          # NOTE: Caching this screws up the order
          # return @full_i18n_scope if @full_i18n_scope

          @full_i18n_scope = original_i18n_scope
          @full_i18n_scope += Array.wrap(i18n_scope)
          @full_i18n_scope += Array.wrap(@scope) if @scope
          @full_i18n_scope += Array.wrap(options.delete(:scope))
          @full_i18n_scope.flatten.compact!

          @full_i18n_scope
        end

        def original_virtual_path
          original_view_context.instance_variable_get(:@virtual_path)
        end

        def original_i18n_scope
          original_virtual_path.sub(%r{^/}, '').gsub(%r{/_?}, '.').split('.')
        end

        def i18n_scope
          self.class.name.split('::').second.underscore.to_sym
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
satis-2.1.43 lib/satis/concerns/contextual_translations.rb
satis-2.1.42 lib/satis/concerns/contextual_translations.rb
satis-2.1.41 lib/satis/concerns/contextual_translations.rb
satis-2.1.40 lib/satis/concerns/contextual_translations.rb
satis-2.1.39 lib/satis/concerns/contextual_translations.rb
satis-2.1.38 lib/satis/concerns/contextual_translations.rb
satis-2.1.37 lib/satis/concerns/contextual_translations.rb
satis-2.1.36 lib/satis/concerns/contextual_translations.rb
satis-2.1.35 lib/satis/concerns/contextual_translations.rb
satis-2.1.33 lib/satis/concerns/contextual_translations.rb
satis-2.1.31 lib/satis/concerns/contextual_translations.rb
satis-2.1.30 lib/satis/concerns/contextual_translations.rb
satis-2.1.29 lib/satis/concerns/contextual_translations.rb
satis-2.1.28 lib/satis/concerns/contextual_translations.rb
satis-2.1.27 lib/satis/concerns/contextual_translations.rb
satis-2.1.26 lib/satis/concerns/contextual_translations.rb
satis-2.1.24 lib/satis/concerns/contextual_translations.rb
satis-2.1.23 lib/satis/concerns/contextual_translations.rb
satis-2.1.22 lib/satis/concerns/contextual_translations.rb
satis-2.1.21 lib/satis/concerns/contextual_translations.rb