Sha256: 25040d2cd6df87f29153ce4b8d17a300246bb950ac9ac7e40413f60b59b1790d

Contents?: true

Size: 1.42 KB

Versions: 58

Compression:

Stored size: 1.42 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Traits
    module Localisation
    private

      def localised_text(context)
        return unless @object_name.present? && @attribute_name.present?

        localise(context) || localise_html(context)
      end

      def localise(context)
        I18n.translate(schema(context), default: nil)
      end

      def localise_html(context)
        I18n.translate("#{schema(context)}_html", default: nil).try(:html_safe)
      end

      def schema(context)
        schema_root(context)
          .push(*schema_path)
          .map { |e| e == :__context__ ? context : e }
          .join('.')
      end

      def schema_path
        if @value.present?
          [@object_name, "#{@attribute_name}_options", @value]
        else
          [@object_name, @attribute_name]
        end
      end

      def schema_root(context)
        contextual_schema = case context
                            when :legend
                              config.localisation_schema_legend
                            when :hint
                              config.localisation_schema_hint
                            when :label
                              config.localisation_schema_label
                            when :caption
                              config.localisation_schema_caption
                            end

        (contextual_schema || config.localisation_schema_fallback).dup
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-3.3.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.2.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.1.2 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.1.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.1.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.3 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.3b1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.2 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.0b2 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-3.0.0b1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.8.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.6 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.5 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.4 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.3 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.2 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-2.7.0 lib/govuk_design_system_formbuilder/traits/localisation.rb