Module: TmlRails::ActionViewExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/tml_rails/extensions/action_view_extension.rb

Instance Method Summary collapse

Instance Method Details

#tml_dir_attribute_tag(lang = tml_current_language) ⇒ Object



190
191
192
193
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 190

def tml_dir_attribute_tag(lang = tml_current_language)
  return "dir='ltr'" if Tml.config.disabled?
  "dir='#{lang.dir}'".html_safe
end

#tml_flashes_tag(opts = {}) ⇒ Object



113
114
115
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 113

def tml_flashes_tag(opts = {})
  render(:partial => '/tml_rails/tags/flashes', :locals => {:opts => opts})
end

#tml_lang_attribute_tag(lang = tml_current_language) ⇒ Object



195
196
197
198
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 195

def tml_lang_attribute_tag(lang = tml_current_language)
  return "lang='en-US'" if Tml.config.disabled?
  "lang='#{lang.locale}'".html_safe
end

#tml_language_flag_tag(lang = tml_current_language, opts = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 58

def tml_language_flag_tag(lang = tml_current_language, opts = {})
  return '' unless tml_application.feature_enabled?(:language_flags)
  html = image_tag(lang.flag_url, :style => (opts[:style] || 'vertical-align:middle;'), :title => lang.native_name)
  html << '&nbsp;&nbsp;'.html_safe
  html.html_safe
end

#tml_language_name_tag(lang = tml_current_language, opts = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 65

def tml_language_name_tag(lang = tml_current_language, opts = {})
  show_flag = opts[:flag].nil? ? true : opts[:flag]
  name_type = opts[:language].nil? ? :english : opts[:language] # :full, :native, :english, :locale, :both

  html = []
  html << "<span style='white-space: nowrap'>"
  html << tml_language_flag_tag(lang, opts) if show_flag
  html << "<span dir='ltr'>"

  if name_type == :both
    html << lang.english_name.to_s
    html << '<span class="trex-native-name">'
    html << lang.native_name
    html << '</span>'
  else
    name = case name_type
      when :native  then lang.native_name
      when :full then lang.full_name
      when :locale  then lang.locale
      else lang.english_name
    end
    html << name.to_s
  end

  html << '</span></span>'
  html.join().html_safe
end

#tml_language_selector_tag(type = nil, opts = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 93

def tml_language_selector_tag(type = nil, opts = {})
  return unless Tml.config.enabled?

  type ||= :default
  unless [:bootstrap, :default, :inline, :select, :popup].include?(type.to_sym)
    return "Unsupported language selector #{type}"
  end

  render(:partial => "/tml_rails/tags/language_selector_#{type}", :locals => {:opts => opts})
end

#tml_language_strip_tag(opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 104

def tml_language_strip_tag(opts = {})
  opts[:flag] = opts[:flag].nil? ? false : opts[:flag]
  opts[:name] = opts[:name].nil? ? :native : opts[:name] 
  opts[:linked] = opts[:linked].nil? ? true : opts[:linked] 
  opts[:javascript] = opts[:javascript].nil? ? false : opts[:javascript] 

  render(:partial => '/tml_rails/tags/language_strip', :locals => {:opts => opts})
end

#tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language) ⇒ Object



45
46
47
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 45

def tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language)
  options_for_select(options.tro(description), selected)
end


49
50
51
52
53
54
55
56
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 49

def tml_phrases_link_tag(search = "", phrase_type = :without, phrase_status = :any)
  return unless Tml.config.enabled?
  return if tml_current_language.default?
  return unless tml_current_translator.inline?

  link_to(image_tag(tml_application.url_for("/assets/tml/translate_icn.gif"), :style => "vertical-align:middle; border: 0px;", :title => search),
          tml_application.url_for("/tml/app/phrases/index?search=#{search}")).html_safe
end

#tml_scripts_tag(opts = {}) ⇒ Object



117
118
119
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 117

def tml_scripts_tag(opts = {})
  render(:partial => '/tml_rails/tags/scripts', :locals => {:opts => opts})
end

#tml_select_month(date, options = {}, html_options = {}) ⇒ Object



121
122
123
124
125
126
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 121

def tml_select_month(date, options = {}, html_options = {})
  month_names = options[:use_short_month] ? Tml.config.default_abbr_month_names : Tml.config.default_month_names
  select_month(date, options.merge(
    :use_month_names => month_names.collect{|month_name| tml_current_language.translate(month_name, options[:description] || "Month name")}
  ), html_options)
end

#tml_style_attribute_tag(attr_name = 'float', default = 'right', lang = tml_current_language) ⇒ Object

Language Direction Support



180
181
182
183
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 180

def tml_style_attribute_tag(attr_name = 'float', default = 'right', lang = tml_current_language)
  return "#{attr_name}:#{default}".html_safe if Tml.config.disabled?
  "#{attr_name}:#{lang.align(default)}".html_safe
end

#tml_style_directional_attribute_tag(attr_name = 'padding', default = 'right', value = '5px', lang = tml_current_language) ⇒ Object



185
186
187
188
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 185

def tml_style_directional_attribute_tag(attr_name = 'padding', default = 'right', value = '5px', lang = tml_current_language)
  return "#{attr_name}-#{default}:#{value}".html_safe if Tml.config.disabled?
  "#{attr_name}-#{lang.align(default)}:#{value}".html_safe
end

#tml_url_tag(path) ⇒ Object



172
173
174
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 172

def tml_url_tag(path)
  tml_application.url_for(path)
end

#tml_when_string_tag(time, opts = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 145

def tml_when_string_tag(time, opts = {})
  elapsed_seconds = Time.now - time
  if elapsed_seconds < 0
    tr('In the future, Marty!', 'Time reference')
  elsif elapsed_seconds < 2.minutes
    tr('a moment ago', 'Time reference')
  elsif elapsed_seconds < 55.minutes
    elapsed_minutes = (elapsed_seconds / 1.minute).to_i
    tr('{minutes || minute} ago', 'Time reference', :minutes => elapsed_minutes)
  elsif elapsed_seconds < 1.75.hours
    tr('about an hour ago', 'Time reference')
  elsif elapsed_seconds < 12.hours
    elapsed_hours = (elapsed_seconds / 1.hour).to_i
    tr('{hours || hour} ago', 'Time reference', :hours => elapsed_hours)
  elsif time.today_in_time_zone?
    display_time(time, :time_am_pm)
  elsif time.yesterday_in_time_zone?
    tr("Yesterday at {time}", 'Time reference', :time => time.tr(:time_am_pm).gsub('/ ', '/').sub(/^[0:]*/,""))
  elsif elapsed_seconds < 5.days
    time.tr(:day_time).gsub('/ ', '/').sub(/^[0:]*/,"")
  elsif time.same_year_in_time_zone?
    time.tr(:monthname_abbr_time).gsub('/ ', '/').sub(/^[0:]*/, '')
  else
    time.tr(:monthname_abbr_year_time).gsub('/ ', '/').sub(/^[0:]*/, '')
  end
end

#tml_with_options_tag(opts, &block) ⇒ Object Also known as: tml_block



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 128

def tml_with_options_tag(opts, &block)
  if Tml.config.disabled?
    return capture(&block) if block_given?
    return ""
  end

  Tml.session.push_block_options(opts)

  if block_given?
    ret = capture(&block)
  end

  Tml.session.pop_block_options
  ret
end

#trh(tokens = {}, options = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 36

def trh(tokens = {}, options = {}, &block)
  return '' unless block_given?

  label = capture(&block)

  tokenizer = Tml::Tokenizers::Dom.new(tokens, options)
  tokenizer.translate(label).html_safe
end