Module: RailsI18nterface::TranslateHelper

Defined in:
app/helpers/rails_i18nterface/translate_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_namespace(h) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/rails_i18nterface/translate_helper.rb', line 39

def build_namespace(h)
  out = '<ul>'
  dirs = {}
  root = []
  h.each do |k, v|
    if v.is_a? Hash
      dirs[k] = v
    else
      root << k
    end
  end
  out << '<li class="dir"><span class="display" data-id="."></span>ROOT'
  out << " <span class=\"num\">(#{root.length})</span>"
  out << '<ul>'
  root.each do |key|
    out << "<li class=\"item\" data-id=\"#{key.to_s}\">#{key}</li>"
  end
  out << '</ul>'
  out << '</ul>'
  out << list_namespace('', dirs)
end

#list_namespace(k, h) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/rails_i18nterface/translate_helper.rb', line 61

def list_namespace(k, h)
  out = '<ul>'
  k != '' && k += '.'
  h.each do |key, val|
    if val.is_a? Hash
      out << "<li class=\"dir\"><span class=\"display\" data-id=\"#{k + key.to_s}\"></span>"
      out << key.to_s
      out << " <span class=\"num\">(#{val.length})</span>"
      out << list_namespace(k + key.to_s, val)
    else
      out << "<li class=\"item\" data-id=\"#{k + key.to_s}\">#{key}"
    end
    out << '</li>'
  end
  out << '</ul>'
end

#lookup(locale, key) ⇒ Object



6
7
8
# File 'app/helpers/rails_i18nterface/translate_helper.rb', line 6

def lookup(locale, key)
  I18n.backend.send(:lookup, locale, key)
end

#n_lines(text, line_size) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/helpers/rails_i18nterface/translate_helper.rb', line 30

def n_lines(text, line_size)
  n_lines = 1
  if text.present?
    n_lines = text.split("\n").size
    n_lines = text.length / line_size + 1 if n_lines == 1 && text.length > line_size
  end
  n_lines
end

#simple_filter(labels, param_name = 'filter', selected_value = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/rails_i18nterface/translate_helper.rb', line 10

def simple_filter(labels, param_name = 'filter', selected_value = nil)
  selected_value ||= params[param_name]
  filter = []
  labels.each do |item|
    if item.is_a?(Array)
      type, label = item
    else
      type = label = item
    end
    if type.to_s == selected_value.to_s
      filter << "<i>#{label}</i>"
    else
      link_params = params.merge({param_name.to_s => type})
      link_params.merge!({'page' => nil}) if param_name.to_s != 'page'
      filter << link_to(label, link_params)
    end
  end
  filter.join(' | ')
end