Class: PufferPages::Liquid::Tags::Translate
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- PufferPages::Liquid::Tags::Translate
- Defined in:
- lib/puffer_pages/liquid/tags/translate.rb
Constant Summary collapse
- Syntax =
/^(#{::Liquid::QuotedFragment})/
Instance Method Summary collapse
- #array_to_key(*array) ⇒ Object
- #i18n_defaults(processed, key) ⇒ Object
- #i18n_key(processed, key) ⇒ Object
-
#initialize(tag_name, markup, tokens) ⇒ Translate
constructor
A new instance of Translate.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Translate
Returns a new instance of Translate.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/puffer_pages/liquid/tags/translate.rb', line 8 def initialize(tag_name, markup, tokens) if markup =~ Syntax @key = $1 else raise SyntaxError.new("Syntax Error in 'translate' - Valid syntax: translate key") end @options = {} markup.scan(::Liquid::TagAttributes) do |key, value| @options[key.to_sym] = value end super end |
Instance Method Details
#array_to_key(*array) ⇒ Object
53 54 55 |
# File 'lib/puffer_pages/liquid/tags/translate.rb', line 53 def array_to_key *array array.flatten.map { |segment| segment.to_s.gsub(?., ?/) }.join(?.).to_sym end |
#i18n_defaults(processed, key) ⇒ Object
49 50 51 |
# File 'lib/puffer_pages/liquid/tags/translate.rb', line 49 def i18n_defaults(processed, key) processed.i18n_defaults.map { |default| array_to_key default, key } end |
#i18n_key(processed, key) ⇒ Object
45 46 47 |
# File 'lib/puffer_pages/liquid/tags/translate.rb', line 45 def i18n_key(processed, key) array_to_key processed.i18n_scope, key end |
#render(context) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/puffer_pages/liquid/tags/translate.rb', line 23 def render(context) key = context[@key] = @options.each_with_object({}) do |(name, value), result| result[name] = context[value] unless I18n::RESERVED_KEYS.include?(name) end processed = context[:processed] if [:count].is_a?(String) begin [:count] = ([:count] =~ /\./ ? Float([:count]) : Integer([:count])) rescue ArgumentError end end if processed && key.first == '.' I18n.translate i18n_key(processed, key.last(-1)), .merge!(:default => i18n_defaults(processed, key.last(-1))) else I18n.translate key, end end |