#-- # Copyright (c) 2010-2011 Michael Berkovich # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ module Tr8n module ActionViewExtension extend ActiveSupport::Concern module InstanceMethods # generates translations json for client sdk def tr8n_translations_js_tag(opts = {}) html = [] html << "" html.join('').html_safe end # translation functions def tr(label, desc = "", tokens = {}, options = {}) if desc.is_a?(Hash) options = desc tokens = options[:tokens] || {} desc = options[:context] || "" end begin url = request.url host = request.env['HTTP_HOST'] source = "#{controller.class.name.underscore.gsub("_controller", "")}/#{controller.action_name}" rescue source = self.class.name url = nil host = 'localhost' end options.merge!(:source => source) unless options[:source] options.merge!(:caller => caller) options.merge!(:url => url) options.merge!(:host => host) unless Tr8n::Config.enabled? return Tr8n::TranslationKey.substitute_tokens(label, tokens, options) end Tr8n::Config.current_language.translate(label, desc, tokens, options) end # for translating labels def trl(label, desc = "", tokens = {}, options = {}) tr(label, desc, tokens, options.merge(:skip_decorations => true)) end def tr8n_options_for_select(options, selected = nil, description = nil, lang = Tr8n::Config.current_language) options_for_select(options.tro(description), selected) end def tr8n_phrases_link_tag(search = "", phrase_type = :without, phrase_status = :any) return unless Tr8n::Config.enabled? return if Tr8n::Config.current_language.default? return unless Tr8n::Config.open_registration_mode? or Tr8n::Config.current_user_is_translator? return unless Tr8n::Config.current_translator.enable_inline_translations? link_to(image_tag("tr8n/translate_icn.gif", :style => "vertical-align:middle; border: 0px;", :title => search), :controller => "/tr8n/phrases", :action => :index, :search => search, :phrase_type => phrase_type, :phrase_status => phrase_status).html_safe end def tr8n_dir_attribute_tag "dir='<%=Tr8n::Config.current_language.dir%>'".html_safe end def tr8n_splash_screen_tag html = "
" html.html_safe end def tr8n_language_flag_tag(lang = Tr8n::Config.current_language, opts = {}) return "" unless Tr8n::Config.enable_language_flags? html = image_tag("tr8n/flags/#{lang.flag}.png", :style => "vertical-align:middle;", :title => lang.native_name) html << " ".html_safe html.html_safe end def tr8n_language_name_tag(lang = Tr8n::Config.current_language, opts = {}) show_flag = opts[:flag].nil? ? true : opts[:flag] name_type = opts[:name].nil? ? :full : opts[:name] # :full, :native, :english, :locale linked = opts[:linked].nil? ? true : opts[:linked] html = "" html << tr8n_language_flag_tag(lang, opts) if show_flag name = case name_type when :native then lang.native_name when :english then lang.english_name when :locale then lang.locale else lang.full_name end if linked html << link_to(name.html_safe, :controller => "/tr8n/language", :action => :switch, :language_action => :switch_language, :locale => lang.locale, :source_url => opts[:source_url]) else html << name end html << "" html.html_safe end def tr8n_language_selector_tag(opts = {}) opts[:lightbox] ||= false opts[:style] ||= "color:#1166bb;" opts[:show_arrow] ||= true opts[:arrow_style] ||= "font-size:8px;" render(:partial => '/tr8n/common/language_selector', :locals => {:opts => opts}) end def tr8n_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 => '/tr8n/common/language_strip', :locals => {:opts => opts}) end def tr8n_language_table_tag(opts = {}) opts[:cols] = opts[:cols].nil? ? 4 : opts[:cols] opts[:col_size] = opts[:col_size].nil? ? "300px" : opts[:col_size] render(:partial => '/tr8n/common/language_table', :locals => {:opts => opts.merge(:name => :english)}) end def tr8n_translator_login_tag(opts = {}) opts[:class] ||= 'tr8n_right_horiz_list' render(:partial => '/tr8n/common/translator_login', :locals => {:opts => opts}) end def tr8n_flashes_tag(opts = {}) render(:partial => '/tr8n/common/flashes', :locals => {:opts => opts}) end def tr8n_scripts_tag(opts = {}) render(:partial => '/tr8n/common/scripts', :locals => {:opts => opts}) end def tr8n_client_sdk_scripts_tag(opts = {}) opts[:default_source] ||= "application" opts[:scheduler_interval] ||= 5000 opts[:enable_inline_translations] = (Tr8n::Config.current_user_is_translator? and Tr8n::Config.current_translator.enable_inline_translations? and (not Tr8n::Config.current_language.default?)) opts[:default_decorations] = Tr8n::Config.default_decoration_tokens opts[:default_tokens] = Tr8n::Config.default_data_tokens opts[:rules] = { :number => Tr8n::Config.rules_engine[:numeric_rule], :gender => Tr8n::Config.rules_engine[:gender_rule], :list => Tr8n::Config.rules_engine[:gender_list_rule], :date => Tr8n::Config.rules_engine[:date_rule] } html = [javascript_include_tag("/tr8n/javascripts/tr8n_client_sdk.js")] html << "" html.join("\n").html_safe end def tr8n_translator_rank_tag(translator, rank = nil) return "" unless translator rank ||= translator.rank || 0 html = "" 1.upto(5) do |i| if rank > i * 20 - 10 and rank < i * 20 html << image_tag("tr8n/rating_star05.png") elsif rank < i * 20 - 10 html << image_tag("tr8n/rating_star0.png") else html << image_tag("tr8n/rating_star1.png") end end html << "" html.html_safe end def tr8n_help_icon_tag(filename = "index") link_to(image_tag("tr8n/help.png", :style => "border:0px; vertical-align:middle;", :title => trl("Help")), {:controller => "/tr8n/help", :action => filename}, :target => "_new").html_safe end def tr8n_help_link(text, opts = {}) filename = opts[:filename].nil? ? text.downcase.gsub(' ', '_') : opts[:filename] classname = "tr8n_selected" if filename == controller.action_name link_to(text, { :controller => "/tr8n/help", :action => filename }, :class => classname).html_safe end def tr8n_spinner_tag(id = "spinner", label = nil, cls='spinner') html = " " html.html_safe end def tr8n_toggler_tag(content_id, label = "", open = true) html = "" html << link_to_function("#{image_tag("tr8n/arrow_down.gif", :style=>'text-align:center; vertical-align:middle')} #{label}".html_safe, "Tr8n.Effects.hide('#{content_id}_open'); Tr8n.Effects.show('#{content_id}_closed'); Tr8n.Effects.blindUp('#{content_id}');", :style=> "text-decoration:none") html << "" html << "" html << link_to_function("#{image_tag("tr8n/arrow_right.gif", :style=>'text-align:center; vertical-align:middle')} #{label}".html_safe, "Tr8n.Effects.show('#{content_id}_open'); Tr8n.Effects.hide('#{content_id}_closed'); Tr8n.Effects.blindDown('#{content_id}');", :style=> "text-decoration:none") html << "" html.html_safe end def tr8n_sitemap(sections, splitters, options = {}) html = "" html << "" html << generate_sitemap(sections[splitter.first..splitter.last], options) html << " | " end html << "