Module: SearchTranslationsHelper

Included in:
Kakimasu::HomeController, Kakimasu::KeysController
Defined in:
app/helpers/search_translations_helper.rb

Instance Method Summary collapse

Instance Method Details

#find_attribute_keys(file) ⇒ Object

Method searches used active record human attribute name keys in given file



46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/search_translations_helper.rb', line 46

def find_attribute_keys(file)
  array = file.scan(/(\w*).human_attribute_name[(][:](\w*)/).uniq
  keys = []
  # Keys are transformed into correct active record attribute keys form
  array.each do |arr|
    arr = 'activerecord.' + 'attributes.' + arr.first.downcase + '.' + arr.last
    keys.push arr
  end
  keys
end

#find_keys(file, path) ⇒ Object

Method searches used translations keys in a file



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/search_translations_helper.rb', line 21

def find_keys(file, path)
  array = file.scan(/[=|\s][t][\s]*[(]?[\s]*['|:]([.]*\w*)/).uniq
  keys = Array.new

  # If key has '.' in front of it, it should be completed as lazy lookup key.
  array.each_with_index do |key, index|
    if key.first[0] == '.'
      array[index] = key.first.insert(0, prefix(path))
    end
  end
  array
end

#find_missing_translations_keys(array) ⇒ Object

Method returns array of keys with missing translations from given array



35
36
37
38
39
40
41
42
43
# File 'app/helpers/search_translations_helper.rb', line 35

def find_missing_translations_keys(array)
  missing_translations_keys = []

  array.each do |key|
    missing_translations_keys.push key if missing_translations?(key)
  end

  missing_translations_keys
end

#find_model_name_keys(file) ⇒ Object

Method searches used Model name keys in given file



58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/search_translations_helper.rb', line 58

def find_model_name_keys(file)
  array = file.scan(/(\w*).model_name.human/).uniq
  keys = []
  # Keys are transformed into correct active record model name keys form
  array.each do |arr|
    arr = 'activerecord.models.' + arr.first.downcase
    keys.push arr
  end
  keys
end

#missing_translations?(key) ⇒ Boolean

Method returns if key has missing translation in one of locales

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'app/helpers/search_translations_helper.rb', line 10

def missing_translations?(key)
  missing_translation = false
  I18n.available_locales.each do |locale|
    if !I18n.exists?(key, locale)
      missing_translation = true  # If in one of locales key has no translation, method returns true
    end
  end
  missing_translation
end

#prefix(path) ⇒ Object

Method to get prefix for lazy lookup



3
4
5
6
7
# File 'app/helpers/search_translations_helper.rb', line 3

def prefix(path)
  prefix = path.split('/')
  prefix[-1] = prefix.last.split('.').first
  prefix = prefix.join('.')
end

#search_for_translations(category) ⇒ Object

Method returns array with requested translation keys



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/search_translations_helper.rb', line 70

def search_for_translations(category)
  keys = []

  Dir.glob("app/views/**/*").each do |path|
    if File.file?(path)
      file = File.read(path)
      case category
      when 'active_record_attribute_translations'
        keys.push find_attribute_keys(file)
      when 'active_record_model_translations'
        keys.push find_model_name_keys(file)
      when 'all_translation_keys'
        path.slice! "app/views/"
        keys.push find_keys(file, path).push path
      when 'missing_translations'
        path.slice! "app/views/"
        # push in array only those keys which have missing translations
        keys.push find_missing_translations_keys(find_keys(file, path)).push path
      end
    end
  end

  case category
  when 'active_record_attribute_translations'
    keys.flatten.uniq
  when 'active_record_model_translations'
    keys.flatten.uniq
  else
    keys
  end
end