Class: RailsI18nterface::Keys

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rails-i18nterface/keys.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#contains_key?, #deep_merge!, #deep_sort, #deep_stringify_keys, #keys_to_yaml, #remove_blanks, #set_nested, #to_deep_hash, #to_shallow_hash

Constructor Details

#initialize(root_dir, from, to) ⇒ Keys

Returns a new instance of Keys.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rails-i18nterface/keys.rb', line 11

def initialize(root_dir, from, to)
  @root_dir = root_dir
  @files = RailsI18nterface::Sourcefiles.load_files(root_dir)
  @yaml_keys = i18n_keys(I18n.default_locale)
  @from_locale = from
  @to_locale = to
  @all_keys = (@files.keys.map(&:to_s) + @yaml_keys).uniq
  @translations = []
  @namespaces = deep_sort(to_deep_hash(@all_keys))
end

Instance Attribute Details

#all_keysObject (readonly)

Returns the value of attribute all_keys.



9
10
11
# File 'lib/rails-i18nterface/keys.rb', line 9

def all_keys
  @all_keys
end

#filesObject

Returns the value of attribute files.



8
9
10
# File 'lib/rails-i18nterface/keys.rb', line 8

def files
  @files
end

#keysObject

Returns the value of attribute keys.



8
9
10
# File 'lib/rails-i18nterface/keys.rb', line 8

def keys
  @keys
end

#namespacesObject

Returns the value of attribute namespaces.



8
9
10
# File 'lib/rails-i18nterface/keys.rb', line 8

def namespaces
  @namespaces
end

#translationsObject (readonly)

Returns the value of attribute translations.



9
10
11
# File 'lib/rails-i18nterface/keys.rb', line 9

def translations
  @translations
end

#yaml_keysObject (readonly)

Returns the value of attribute yaml_keys.



9
10
11
# File 'lib/rails-i18nterface/keys.rb', line 9

def yaml_keys
  @yaml_keys
end

Class Method Details

.translated_localesObject



133
134
135
136
137
# File 'lib/rails-i18nterface/keys.rb', line 133

def self.translated_locales
  I18n.available_locales.reject do |locale|
    [:root, I18n.default_locale.to_sym].include?(locale)
  end
end

Instance Method Details

#apply_filters(params) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rails-i18nterface/keys.rb', line 22

def apply_filters(params)
  params[:filter] ||= 'all'
  remove_proc
  filter_by_translated(params[:filter]) if params[:filter] != 'all'
  filter_by_key_pattern(params[:key_type], params[:key_pattern]) unless params[:key_pattern].blank?
  filter_by_text_pattern(params[:text_type], params[:text_pattern]) unless params[:text_pattern].blank?
  sort_keys(params[:sort_by])
end

#filter_by_key_pattern(type, pattern) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails-i18nterface/keys.rb', line 48

def filter_by_key_pattern(type, pattern)
  @all_keys.reject! do |key|
    if type == 'starts_with'
      if pattern == '.'
        key.match(/\./)
      else
        !key.starts_with?(pattern)
      end
    else # contains
      key.index(pattern).nil?
    end
  end
end

#filter_by_text_pattern(type, pattern) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rails-i18nterface/keys.rb', line 62

def filter_by_text_pattern(type, pattern)
  @all_keys.reject! do |key|
    lookup_key = lookup(@from_locale, key)
    if type == 'contains'
      !lookup_key.present? || !lookup_key.to_s.downcase.index(pattern.downcase)
    else # equals
      !lookup_key.present? || lookup_key.to_s.downcase != pattern.downcase
    end
  end
end

#filter_by_translated(filter) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rails-i18nterface/keys.rb', line 38

def filter_by_translated(filter)
  @all_keys.reject! do |key|
    if filter == 'untranslated'
      lookup(@to_locale, key).present?
    else # translated
      lookup(@to_locale, key).blank?
    end
  end
end

#i18n_keys(locale) ⇒ Object



95
96
97
98
# File 'lib/rails-i18nterface/keys.rb', line 95

def i18n_keys(locale)
  I18n.backend.send(:init_translations) unless I18n.backend.initialized?
  to_shallow_hash(I18n.backend.send(:translations)[locale.to_sym]).keys.sort
end

#lookup(locale, key) ⇒ Object



129
130
131
# File 'lib/rails-i18nterface/keys.rb', line 129

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

#missing_keysObject



119
120
121
122
123
124
125
126
127
# File 'lib/rails-i18nterface/keys.rb', line 119

def missing_keys
  locale = I18n.default_locale
  filepath = Dir.glob(File.join(@root_dir, 'config', 'locales', '**', "#{locale}.yml"))
  yaml_keys = {}
  yaml_keys = filepath.reduce({}) do |a, e|
    a = a.deep_merge(Yamlfile.new(@root_dir, locale).read[locale.to_s])
  end
  @files.keys.reject { |key, file| contains_key?(yaml_keys, key) }
end

#paginate(page, per_page) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/rails-i18nterface/keys.rb', line 100

def paginate(page, per_page)
  offset = (page - 1) * per_page
  @all_keys[offset, per_page].each do |key|
    translation = RailsI18nterface::Translation.new(key, @from_locale, @to_locale)
    translation.files = @files[key]
    translation.persisted = @yaml_keys.include? key
    @translations << translation
  end
end

#reload(root_dir) ⇒ Object



89
90
91
92
93
# File 'lib/rails-i18nterface/keys.rb', line 89

def reload(root_dir)
  @files = RailsI18nterface::Sourcefiles.refresh(root_dir)
  @yaml_keys = i18n_keys(I18n.default_locale)
  @all_keys = (@files.keys.map(&:to_s) + @yaml_keys).uniq
end

#remove_procObject



31
32
33
34
35
36
# File 'lib/rails-i18nterface/keys.rb', line 31

def remove_proc
  @all_keys.reject! do |key|
    lookup_key = lookup(@from_locale, key)
    lookup_key.is_a? Proc
  end
end

#sort_keys(order = 'key') ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rails-i18nterface/keys.rb', line 73

def sort_keys(order = 'key')
  if order == 'key'
    @all_keys.sort!
  else # text
    @all_keys.sort! do |key1, key2|
      if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
        lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
      elsif lookup(@from_locale, key1).present?
        1
      else
        -1
      end
    end
  end
end

#untranslated_keysObject



110
111
112
113
114
115
116
117
# File 'lib/rails-i18nterface/keys.rb', line 110

def untranslated_keys
  self.class.translated_locales.reduce({}) do |a, e|
    a[e] = i18n_keys(I18n.default_locale).map do |key|
      I18n.backend.send(:lookup, e, key).nil? ? key : nil
    end.compact
    a
  end
end