Module: Haml::MagicTranslations::EngineMethods

Defined in:
lib/haml/magic_translations.rb

Overview

:nodoc:all

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.magic_translations_helpersObject

Returns the value of attribute magic_translations_helpers.



141
142
143
# File 'lib/haml/magic_translations.rb', line 141

def magic_translations_helpers
  @magic_translations_helpers
end

Instance Method Details

#compile_filterObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/haml/magic_translations.rb', line 177

def compile_filter
  super unless magic_translations?

  case @node.value[:name]
    when 'markdown', 'maruku'
      @node.value[:text] = "\#{_(<<-'END_OF_TRANSLATABLE_MARKDOWN'.rstrip
#{@node.value[:text].rstrip.gsub(/\n/, '\n')}
END_OF_TRANSLATABLE_MARKDOWN
)}"
    when 'javascript'
      @node.value[:text].gsub!(/_\(('(([^']|\\')+)'|"(([^"]|\\")+)")\)/) do |m|
        to_parse = $1[1..-2].gsub(/"/, '\"')
        parsed_string = JSON.parse("[\"#{to_parse}\"]")[0]
        parsed_string.gsub!(/'/, "\\\\'")
        "\#{_('#{parsed_string}').to_json}"
      end
  end
  super
end

#compile_rootObject



197
198
199
200
201
202
# File 'lib/haml/magic_translations.rb', line 197

def compile_root
  if magic_translations?
    @precompiled << "extend #{EngineMethods.magic_translations_helpers};"
  end
  super
end

#magic_translations?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
# File 'lib/haml/magic_translations.rb', line 144

def magic_translations?
  return self.options[:magic_translations] unless self.options[:magic_translations].nil?

  Haml::MagicTranslations.enabled?
end

#parse_tag(line) ⇒ Object

Overriden function that parses Haml tags. Injects gettext call for all plain text lines.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/haml/magic_translations.rb', line 152

def parse_tag(line)
  tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
    nuke_inner_whitespace, action, value, last_line = super(line)

  if magic_translations? && !value.empty?
    unless action && action == '=' || action == '!' && value[0] == ?= || action == '&' && value[0] == ?=
      value, interpolation_arguments = Haml::MagicTranslations.prepare_i18n_interpolation(value)
      value = "\#{_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\}\n"
    end
  end
  [tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
     nuke_inner_whitespace, action, value, last_line]
end

#plain(text, escape_html = nil) ⇒ Object

Magical translations will be also used for plain text.



167
168
169
170
171
172
173
174
175
# File 'lib/haml/magic_translations.rb', line 167

def plain(text, escape_html = nil)
  if magic_translations?
    value, interpolation_arguments = Haml::MagicTranslations.prepare_i18n_interpolation(text, escape_html)
    value = "_('#{value.gsub(/'/, "\\\\'")}') % #{interpolation_arguments}\n"
    script(value, !:escape_html)
  else
    super
  end
end