Module: TmlRails::ActionViewExtension
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/tml_rails/extensions/action_view_extension.rb
Instance Method Summary collapse
-
#tml_dir_attribute_tag(lang = tml_current_language) ⇒ Object
providers the direction of the language.
-
#tml_html_attributes_tag(lang = tml_current_language) ⇒ Object
(also: #tml_lang_attributes_tag)
provides the locale and direction of the language.
-
#tml_lang_attribute_tag(lang = tml_current_language) ⇒ Object
provides the locale of the language.
-
#tml_language_flag_tag(lang = tml_current_language, opts = {}) ⇒ Object
Returns language flag.
-
#tml_language_name_tag(lang = tml_current_language, opts = {}) ⇒ Object
Returns language name.
-
#tml_language_selector_tag(type = nil, opts = {}) ⇒ Object
Returns language selector UI.
-
#tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language) ⇒ Object
Translates <select><option></option></select>.
- #tml_scripts_tag(opts = {}) ⇒ Object
- #tml_select_month(date, options = {}, html_options = {}) ⇒ Object
- #tml_source(source, &block) ⇒ Object
-
#tml_style_attribute_tag(attr_name = 'float', default = 'right', lang = tml_current_language) ⇒ Object
switches CSS positions based on the language direction <%= tml_style_attribute_tag(‘float’, ‘right’) %> => “float: right” : “float: left” <%= tml_style_attribute_tag(‘align’, ‘right’) %> => “align: right” : “align: left”.
-
#tml_style_directional_attribute_tag(attr_name = 'padding', default = 'right', value = '5px', lang = tml_current_language) ⇒ Object
supports directional CSS attributes <%= tml_style_directional_attribute_tag(‘padding’, ‘right’, ‘5px’) %> => “padding-right: 5px” : “padding-left: 5px”.
- #tml_stylesheet_link_tag(ltr, rtl, attrs = {}) ⇒ Object
- #tml_url_tag(path) ⇒ Object
- #tml_when_string_tag(time, opts = {}) ⇒ Object
- #tml_with_options_tag(opts, &block) ⇒ Object (also: #tml_block)
-
#trh(tokens = {}, options = {}, &block) ⇒ Object
Translates HTML block noinspection RubyArgCount.
Instance Method Details
#tml_dir_attribute_tag(lang = tml_current_language) ⇒ Object
providers the direction of the language
248 249 250 251 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 248 def tml_dir_attribute_tag(lang = tml_current_language) return "dir='ltr'" if Tml.config.disabled? "dir='#{lang.dir}'".html_safe end |
#tml_html_attributes_tag(lang = tml_current_language) ⇒ Object Also known as: tml_lang_attributes_tag
provides the locale and direction of the language
242 243 244 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 242 def tml_html_attributes_tag(lang = tml_current_language) "xml:lang='#{lang.locale}' #{tml_lang_attribute_tag(lang)} #{tml_dir_attribute_tag(lang)}".html_safe end |
#tml_lang_attribute_tag(lang = tml_current_language) ⇒ Object
provides the locale of the language
254 255 256 257 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 254 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
Returns language flag
53 54 55 56 57 58 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 53 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 << ' '.html_safe html.html_safe end |
#tml_language_name_tag(lang = tml_current_language, opts = {}) ⇒ Object
Returns language name
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 61 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].to_sym # :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
Returns language selector UI
90 91 92 93 94 95 96 97 98 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 90 def tml_language_selector_tag(type = nil, opts = {}) return unless Tml.config.enabled? type ||= :default type = :dropdown if type == :select opts = opts.collect{|key, value| "data-tml-#{key}='#{value}'"}.join(' ') "<div data-tml-language-selector='#{type}' #{opts}></div>".html_safe end |
#tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language) ⇒ Object
Translates <select><option></option></select>
48 49 50 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 48 def (, selected = nil, description = nil, lang = Tml.session.current_language) (.tro(description), selected) end |
#tml_scripts_tag(opts = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 100 def tml_scripts_tag(opts = {}) return '' unless Tml.config.enabled? if opts[:js] js_opts = opts[:js].is_a?(Hash) ? opts[:js] : {} js_host = js_opts[:host] || 'https://tools.translationexchange.com/tml/stable/tml.min.js' html = [] html << "<script src='#{js_host}'></script>" html << '<script>' html << 'tml.init({' html << " key: '#{tml_application.key}', " html << " token: '#{tml_application.token}', " html << " debug: #{js_opts[:debug] || false}," if js_opts[:onload] html << ' onLoad: function() {' html << " #{js_opts[:onload]}" html << ' }' end html << '});' html << '</script>' return html.join.html_safe end agent_config = Tml.config.respond_to?(:agent) ? Tml.config.agent : {} agent_host = agent_config[:host] || 'https://tools.translationexchange.com/agent/stable/agent.min.js' if agent_config[:cache] t = Time.now t = t - (t.to_i % agent_config[:cache].to_i).seconds agent_host += "?ts=#{t.to_i}" end agent_config[:locale] = tml_current_locale agent_config[:source] = tml_current_source agent_config[:css] = tml_application.css agent_config[:sdk] = Tml.respond_to?(:full_version) ? Tml.full_version : Tml::VERSION agent_config[:languages] = [] tml_application.languages.each do |lang| agent_config[:languages] << { locale: lang.locale, english_name: lang.english_name, native_name: lang.native_name, flag_url: lang.flag_url } end html = [] html << '<script>' html << '(function() {' html << "var script = window.document.createElement('script');" html << "script.setAttribute('id', 'tml-agent');" html << "script.setAttribute('type', 'application/javascript');" html << "script.setAttribute('src', '#{agent_host}');" html << "script.setAttribute('charset', 'UTF-8');" html << 'script.onload = function() {' html << " Trex.init('#{tml_application.key}', #{agent_config.to_json.html_safe});" html << '};' html << "window.document.getElementsByTagName('head')[0].appendChild(script);" html << '})();' html << '</script>' html.join.html_safe end |
#tml_select_month(date, options = {}, html_options = {}) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 163 def tml_select_month(date, = {}, = {}) month_names = [:use_short_month] ? Tml.config.default_abbr_month_names : Tml.config.default_month_names select_month(date, .merge( :use_month_names => month_names.collect{|month_name| tml_current_language.translate(month_name, [:description] || "Month name")} ), ) end |
#tml_source(source, &block) ⇒ Object
187 188 189 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 187 def tml_source(source, &block) ({source: source}, &block) end |
#tml_style_attribute_tag(attr_name = 'float', default = 'right', lang = tml_current_language) ⇒ Object
switches CSS positions based on the language direction <%= tml_style_attribute_tag(‘float’, ‘right’) %> => “float: right” : “float: left” <%= tml_style_attribute_tag(‘align’, ‘right’) %> => “align: right” : “align: left”
229 230 231 232 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 229 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
supports directional CSS attributes <%= tml_style_directional_attribute_tag(‘padding’, ‘right’, ‘5px’) %> => “padding-right: 5px” : “padding-left: 5px”
236 237 238 239 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 236 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_stylesheet_link_tag(ltr, rtl, attrs = {}) ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 259 def tml_stylesheet_link_tag(ltr, rtl, attrs = {}) if tml_current_language.right_to_left? stylesheet_link_tag(rtl, attrs) else stylesheet_link_tag(ltr, attrs) end end |
#tml_url_tag(path) ⇒ Object
218 219 220 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 218 def tml_url_tag(path) tml_application.url_for(path) end |
#tml_when_string_tag(time, opts = {}) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 191 def tml_when_string_tag(time, opts = {}) elapsed_seconds = Time.now - time if elapsed_seconds < 0 tr('in {num || second}', 'Time reference', {num: elapsed_seconds * -1}) 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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 170 def (opts, &block) if Tml.config.disabled? return capture(&block) if block_given? return "" end Tml.session.(opts) if block_given? ret = capture(&block) end Tml.session. ret end |
#trh(tokens = {}, options = {}, &block) ⇒ Object
Translates HTML block noinspection RubyArgCount
38 39 40 41 42 43 44 45 |
# File 'lib/tml_rails/extensions/action_view_extension.rb', line 38 def trh(tokens = {}, = {}, &block) return '' unless block_given? label = capture(&block) tokenizer = Tml::Tokenizers::Dom.new(tokens, ) tokenizer.translate(label).html_safe end |