Sha256: 2dba5af130f3ba89cda91eecc5ca26cd9229ae010d30f236a264d6040b0839e1

Contents?: true

Size: 975 Bytes

Versions: 14

Compression:

Stored size: 975 Bytes

Contents

require 'i18n'

module ClassyEnum
  module Translation

    # Returns a translated string of the enum type. Used internally to create
    # the select_options array.
    #
    # Translation location is:
    # locale.classy_enum.base_class.enum_string
    #
    # ==== Example
    #  # Create an Enum with some elements
    #  class Priority < ClassyEnum::Base
    #  end
    #
    #  class Priority::Low < Priority; end
    #  class Priority::ReallyHigh < Priority; end
    #
    #  # Default translations are `to_s.titlieze`
    #  Priority::Low.new.text # => 'Low'
    #  Priority::ReallyHigh.new.text # => 'Really High'
    #
    #  # Assuming we have a translation defined for:
    #  # es.classy_enum.priority.low # => 'Bajo'
    #
    #  Priority::Low.new.text # => 'Bajo'
    def text
      I18n.translate to_s, :scope => i18n_scope, :default => to_s.titleize
    end

    private

    def i18n_scope
      [:classy_enum, base_class.name.underscore]
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
classy_enum-3.5.0 lib/classy_enum/translation.rb
classy_enum-3.5.0.beta2 lib/classy_enum/translation.rb
classy_enum-3.5.0.beta lib/classy_enum/translation.rb
classy_enum-3.4.0 lib/classy_enum/translation.rb
classy_enum-3.4.0.beta1 lib/classy_enum/translation.rb
classy_enum-3.3.2 lib/classy_enum/translation.rb
classy_enum-3.3.1 lib/classy_enum/translation.rb
classy_enum-3.3.0 lib/classy_enum/translation.rb
classy_enum-3.2.1 lib/classy_enum/translation.rb
classy_enum-3.2.0 lib/classy_enum/translation.rb
classy_enum-3.1.3 lib/classy_enum/translation.rb
classy_enum-3.1.2 lib/classy_enum/translation.rb
classy_enum-3.1.1 lib/classy_enum/translation.rb
classy_enum-3.1.0 lib/classy_enum/translation.rb