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
# 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
  @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

#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



115
116
117
118
119
# File 'lib/rails-i18nterface/keys.rb', line 115

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



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

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

#filter_by_key_pattern(type, pattern) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails-i18nterface/keys.rb', line 39

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/rails-i18nterface/keys.rb', line 53

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



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

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



87
88
89
90
# File 'lib/rails-i18nterface/keys.rb', line 87

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



111
112
113
# File 'lib/rails-i18nterface/keys.rb', line 111

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

#missing_keysObject



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

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

#reload(root_dir) ⇒ Object



81
82
83
84
85
# File 'lib/rails-i18nterface/keys.rb', line 81

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

#sort_keys(order) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails-i18nterface/keys.rb', line 64

def sort_keys(order)
  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



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

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