Module: RailsI18nterface::Sourcefiles

Extended by:
Cache
Defined in:
lib/rails-i18nterface/sourcefiles.rb

Class Method Summary collapse

Methods included from Cache

cache_load, cache_save

Class Method Details

.extract_activerecords(root_dir) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rails-i18nterface/sourcefiles.rb', line 85

def extract_activerecords(root_dir)
  files = {}
  schema = File.join(root_dir, 'db', 'schema.rb')
  if File.exists? schema
    regex = Regexp.new('\s*create_table\s"([^"]*)[^\n]*\n(.*?)\send\n', Regexp::MULTILINE)
    matchdata = regex.match(File.read(schema))
    while matchdata != nil
      model = matchdata[1].classify.underscore
      files["activerecord.models.#{model}"] = ['db/schema.rb']
      files.merge!(extract_attributes(model, matchdata[2]))
      matchdata = regex.match(matchdata.post_match)
    end
  end
  files
end

.extract_attributes(model, txt) ⇒ Object



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

def extract_attributes(model, txt)
  files = {}
  regex = Regexp.new('\s*t\.[-_0-9a-z]*\s*"([^"]*?)"')
  matchdata = regex.match(txt)
  while !matchdata.nil?
    attribute = matchdata[1]
    files["activerecord.attributes.#{model}.#{attribute}"] = ['db/schema.rb']
    matchdata = regex.match(matchdata.post_match)
  end
  files
end

.extract_files(root_dir) ⇒ Object



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

def extract_files(root_dir)
  f = {}
  files_to_scan(root_dir).reduce(HashWithIndifferentAccess.new) do |files, file|
    f = files.merge! populate_keys(root_dir, file)
  end
  f.merge extract_activerecords(root_dir)
end

.files_to_scan(root_dir) ⇒ Object



80
81
82
83
# File 'lib/rails-i18nterface/sourcefiles.rb', line 80

def files_to_scan(root_dir)
  Dir.glob(File.join(root_dir, '{app,config,lib}', '**', '*.{rb,erb,haml,slim,rhtml}')) +
    Dir.glob(File.join(root_dir, '{public,app/assets}', 'javascripts', '**', '*.{js,coffee}'))
end

.load_files(root_dir) ⇒ Object



9
10
11
12
13
14
# File 'lib/rails-i18nterface/sourcefiles.rb', line 9

def load_files(root_dir)
  cachefile = File.join(root_dir, 'tmp', 'translation_strings')
  cache_load cachefile, root_dir do |options|
    RailsI18nterface::Sourcefiles.extract_files options
  end
end

.patternObject

old pattern for reference



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

def pattern
  /\b
    (?:I18n\.t|I18n\.translate|t)
    (?:\s+|\():?(?:'|")
      (\.?[a-z0-9_\.]+)
    (?:'|")
    (?:\s*,\s*)?
    (count:|:count\s*=>)?
  /x
end

.pattern2Object

also catch t(:symbol_keys)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rails-i18nterface/sourcefiles.rb', line 61

def pattern2
  /\b
    (?:I18n\.t|I18n\.translate|t)
    (?:\s+|\()(?:
      :?(?:'|")
        (\.?[a-z0-9_\.]+)
      (?:'|")
      |
      (?::)([a-z0-9_]+)
    )
    (?:\s*,\s*)?
    (count:|:count\s*=>)?
  /x
end

.populate_keys(root_dir, file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails-i18nterface/sourcefiles.rb', line 30

def populate_keys(root_dir, file)
  files = {}
  IO.read(file).scan(pattern2).each do |key, key2, count|
    key = key || key2
    path = relative_path(root_dir, file)
    if count
      keys = [key + '.zero', key + '.one', key + '.other']
    else
      keys = [key.to_s]
    end
    keys.map(&:to_sym).each do |k|
      files[k] ||= []
      files[k] << path unless files[k].include?(path)
    end
  end
  files
end

.refresh(root_dir) ⇒ Object



16
17
18
19
20
# File 'lib/rails-i18nterface/sourcefiles.rb', line 16

def refresh(root_dir)
  cachefile = File.join(root_dir, 'tmp', 'translation_strings')
  FileUtils.rm cachefile if File.exists? cachefile
  load_files(root_dir)
end

.relative_path(root_dir, file) ⇒ Object



76
77
78
# File 'lib/rails-i18nterface/sourcefiles.rb', line 76

def relative_path(root_dir, file)
  Pathname.new(File.expand_path(file)).relative_path_from(Pathname.new(root_dir)).to_s
end