Sha256: 988cb0779aa8d805c6e0d3fd5cc132d3ab6fa7e1a177462074f10744fd38d370

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Traits
    module Localisation
      # starts with an letter that is followed by other word characters or
      # spaces, zero or more times
      BASE_NAME_REGEXP = %r{[[:alpha:]](?:[\w\s]*)}

    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
        base_object_name = if config.enable_nested_localisation
                             @object_name.to_s.scan(BASE_NAME_REGEXP).join(".")
                           else
                             @object_name
                           end

        if @value.present?
          [base_object_name, "#{@attribute_name}_options", @value]
        else
          [base_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

15 entries across 15 versions & 2 rubygems

Version Path
govuk_design_system_formbuilder-5.9.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.8.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.7.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.7.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.6.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.5.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.4.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/govuk_design_system_formbuilder-5.4.0/lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.3.3 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.3.2 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.3.1 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.3.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.2.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.1.0 lib/govuk_design_system_formbuilder/traits/localisation.rb
govuk_design_system_formbuilder-5.1.0b1 lib/govuk_design_system_formbuilder/traits/localisation.rb