Module: TranslatablesHelper

Defined in:
lib/translatables_helper.rb

Instance Method Summary collapse

Instance Method Details

#available_in_locales_for(translatable, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/translatables_helper.rb', line 2

def available_in_locales_for(translatable, options = {})
  return if TranslationsHelper.available_locales.size < 2

  html = "<ul style='list-style:none; margin:0; padding:0;'>"
  html += "<li style='float:left;'>#{I18n.t('translations.helpers.available_in')}</li>"
  options[:params] ||= {}

  translated_in_locales = Array.new
  translatable.available_in_these_locales.each_with_index do |locale, index|
    styles = "float: left; padding: 0 5px; #{'border-left: 1px solid #000' unless index == 0}"
    onclick = 'update_translation_box(this); return false' if options[:lightbox]

    html += (:li, :style => styles) do
      link_to_unless_current(TranslationsHelper::available_locales[locale],
        url_for(:locale => locale, :to_locale => (params[:to_locale] if defined?(params))),
        { :onclick => onclick })
    end
    translated_in_locales << locale unless locale == translatable.original_locale
  end

  manage_links = manage_translations_links_for(translatable, translated_in_locales, options)

  if translated_in_locales.size > 0
    html += "<li style='float:left; padding-left: 25px;'>(#{manage_links})</li>"
  end

  html += '</ul>'
  html += "<div style='clear:both;'></div>"

  translatable_lightbox_js_and_css if options[:lightbox]
  google_auto_translatable_js

  html
end

#google_auto_translatable_jsObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/translatables_helper.rb', line 210

def google_auto_translatable_js
  return unless MongoTranslatableConfiguration.provide_autotranslate
  return if @google_auto_translatable_js_inserted

  js = javascript_include_tag("http://www.google.com/jsapi?format=")
  js += javascript_tag("
  google.load('language', '1');
  function getGoogleTranslation(field_id, text, from_language, to_language) {
    google.language.translate(text, from_language, to_language, function(result) {
      if (!result.error) { Form.Element.setValue(field_id, result.translation); }
    });
  }
  ")

  @google_auto_translatable_js_inserted = true
  content_for(:add_on_scripts_and_links) { js }
end


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/translatables_helper.rb', line 37

def manage_translations_links_for(translatable, translated_in_locales, options)
  manage_links = String.new

  # we only have one locale beyond the original locale
  # just give its edit and delete links
  if translated_in_locales.size == 1
    manage_links = link_to(I18n.t('translations.helpers.edit'), {
                             :controller => :translations,
                             :action => :edit,
                             :id => translated_in_locales.first,
                             translatable_key_from(translatable) => translatable
                           }.merge(options[:params]))
    manage_links += " | " + link_to(I18n.t('translations.helpers.delete'), {
                                      :controller => :translations,
                                      :action => :destroy,
                                      :id => translated_in_locales.first,
                                      :return_to_translated => true,
                                       translatable_key_from(translatable) => translatable
                                     }.merge(options[:params]), {
                                      :method => :delete,
                                      :confirm => t('translations.helpers.are_you_sure') })
  elsif translated_in_locales.size > 1
    manage_links = link_to(I18n.t('translations.helpers.manage'), {
                             :controller => :translations,
                             :action => :index,
                             translatable_key_from(translatable) => translatable
                           })
  end
end

#needed_in_locales_for(translatable, options = {}) ⇒ Object



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

def needed_in_locales_for(translatable, options = {})
  return if TranslationsHelper.available_locales.size < 2

  html = "<ul style='list-style:none; margin:0; padding:0;'>"
  html += "<li style='float:left;'>#{I18n.t('translations.helpers.needs_translating_to')}</li>"

  needed_locales_links = needed_in_locales_links(translatable, options)
  return if needed_locales_links.blank?

  needed_locales_links.each_with_index do |link, index|
    styles = "float: left; padding: 0 10px; #{'border-left: 1px solid #000' unless index == 0}"
    html += (:li, :style => styles) do
      link
    end
  end

  html += '</ul>'
  html += "<div style='clear:both;'></div>"

  translatable_lightbox_js_and_css if options[:lightbox]
  google_auto_translatable_js

  html
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/translatables_helper.rb', line 92

def needed_in_locales_links(translatable, options = {})
  return if TranslationsHelper.available_locales.size < 2
  needed_locales = translatable.needed_in_these_locales
  return unless needed_locales.any?
  options[:params] ||= {}
  links = needed_locales.collect do |locale|
    onclick = 'update_translation_box(this); return false' if options[:lightbox]
    link_to(TranslationsHelper::available_locales[locale],
            { :action => :new,
              :controller => :translations,
              translatable_key_from(translatable) => translatable,
              :to_locale => locale }.merge(options[:params]), { :onclick => onclick })
  end
  links
end


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/translatables_helper.rb', line 119

def raw_available_in_locales_links(translatable, options = {})
  onclick = 'update_translation_box(this); return false' if options[:lightbox]
  links = translatable.available_in_these_locales.collect do |locale|
    if locale == I18n.locale
      { :link => TranslationsHelper::available_locales[locale],
        :locale => locale }
    else
      { :link => link_to(TranslationsHelper::available_locales[locale],
                         url_for(:locale => locale,
                                 :to_locale => (params[:to_locale] if defined?(params))),
                         { :onclick => onclick }),
        :locale => locale }
    end
  end
  links
end

Links to all locales



110
111
112
113
114
115
116
117
# File 'lib/translatables_helper.rb', line 110

def raw_locale_links(options = {})
   links = TranslationsHelper::available_locales.keys.collect do |locale|
     {:link => link_to_unless_current(TranslationsHelper::available_locales[locale],
        url_for(:locale => locale, :to_locale => (params[:to_locale] if defined?(params)))
      ), :locale => locale}
   end
   links
end

#translatable_key_from(translatable) ⇒ Object



153
154
155
# File 'lib/translatables_helper.rb', line 153

def translatable_key_from(translatable)
  translatable.class.name.tableize.singularize + '_id'
end

#translatable_lightbox_js_and_cssObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/translatables_helper.rb', line 157

def translatable_lightbox_js_and_css
  return if @translatable_lightbox_js_and_css_inserted

  js = javascript_tag("
  function close_open_translation_box() {
    if ($('translate_outer_box')) { $('translate_outer_box').remove(); }
    if ($('translate_inner_box')) { $('translate_inner_box').remove(); }
  }

  function page_dimensions() {
    var html_dimensions = $$('html').first().getDimensions();
    var body_dimensions = document.body.getDimensions();

    var dimensions = new Array();
    dimensions['width'] = (html_dimensions['width'] > body_dimensions['width'] ? html_dimensions['width'] : body_dimensions['width']);
    dimensions['height'] = (html_dimensions['height'] > body_dimensions['height'] ? html_dimensions['height'] : body_dimensions['height']);
    return dimensions;
  }

  function update_translation_box(element) {
    new Ajax.Request(element.href, {
      method: 'get',
      onComplete: function(transport) {
        var outer_box = new Element('div', { 'id': 'translate_outer_box' }).setOpacity(0.8).setStyle({ width: page_dimensions()['width'] + 'px', height: page_dimensions()['height'] + 'px' });
        var close_link = '<a href=\\'\\' name=\\'top\\' title=\\'Close\\' onclick=\\'close_open_translation_box(); return false;\\'>#{I18n.t('translations.helpers.close_box')}</a>';
        var close_box = '<div id=\\'translate_close_box\\'>' + close_link + '</div>';
        var inner_box = new Element('div', { 'id': 'translate_inner_box' }).update(close_box + transport.responseText);
        close_open_translation_box();
        document.body.appendChild(outer_box);
        document.body.appendChild(inner_box);
        window.location.hash = 'top'; // send us to the top of the translation box
        window.onresize = function() {
          $('translate_outer_box').setStyle({ width: page_dimensions()['width'] + 'px', height: page_dimensions()['height'] + 'px' });
        }
      }
    });
  }
  ")

  css = ("style", :type => "text/css") do
    <<-CSS
    #translate_outer_box { position: absolute; top: 0; left: 0; width: 100%; min-height: 500px; background-color: #000; }
    #translate_inner_box { position: absolute; top: 0; left: 15%; right: 15%; margin: 50px auto; padding: 20px;
                           background-color: #fff; -moz-border-radius: 1em; -webkit-border-radius: 1em; }
    #translate_close_box { float: right; margin-top: -45px; margin-right: -15px; }
    #translate_close_box a { color: #fff; }
    CSS
  end

  @translatable_lightbox_js_and_css_inserted = true
  content_for(:add_on_scripts_and_links) { js + css }
end

Translate link for the given locale



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/translatables_helper.rb', line 137

def translate_link(translatable, options = {})
  options[:params] ||= {}
  options[:onclick] = 'update_translation_box(this); return false' if options[:lightbox]
  options[:onclick] ||= ""
  
  action = translatable.translation_for(params[:locale]) ? :edit : :new
  action = options[:action].present? ? options[:action] : action

  options[:params][:id] = params[:locale] if action == :edit
  link_to(I18n.t('translations.helpers.translate'),
          { :action => action,
            :controller => :translations,
            translatable_key_from(translatable) => translatable,
            :to_locale => params[:locale] }.merge(options[:params]), :onclick => options[:onclick])
end