Class: MissingT

Inherits:
Object
  • Object
show all
Defined in:
lib/missing_t.rb

Defined Under Namespace

Classes: FileReader

Constant Summary collapse

VERSION =
"0.4.1"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MissingT

Returns a new instance of MissingT.



61
62
63
64
65
# File 'lib/missing_t.rb', line 61

def initialize(options={})
  @reader = options.fetch(:reader, FileReader.new)
  @languages = options[:languages]
  @path = options[:path]
end

Instance Method Details

#collect_missingObject



82
83
84
85
86
87
88
# File 'lib/missing_t.rb', line 82

def collect_missing
  ts = translation_keys
  #TODO: If no translation keys were found and the languages were not given explicitly
  # issue a warning and bail out
  languages = @languages ? @languages : ts.keys
  get_missing_translations(translation_keys, translation_queries, languages)
end

#extract_i18n_queries(file) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/missing_t.rb', line 151

def extract_i18n_queries(file)
  ({}).tap do |queries|
    @reader.read(File.expand_path(file)) do |line|
      qs = scan_line(line)
      queries.merge!(qs)
    end
  end
end

#files_with_i18n_queriesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/missing_t.rb', line 127

def files_with_i18n_queries
  if @path
    path = File.expand_path(@path)
    if File.file?(path)
      [@path]
    else
      path.chomp!('/')
      [
        Dir.glob("#{path}/**/*.erb"),
        Dir.glob("#{path}/**/*.haml"),
        Dir.glob("#{path}/**/*.rb")
      ]
    end
  else
    [
      Dir.glob("app/**/*.erb"),
      Dir.glob("app/**/*.haml"),
      Dir.glob("app/**/models/**/*.rb"),
      Dir.glob("app/**/controllers/**/*.rb"),
      Dir.glob("app/**/helpers/**/*.rb")
    ]
  end.flatten
end

#get_missing_translations(keys, queries, languages) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/missing_t.rb', line 90

def get_missing_translations(keys, queries, languages)
  languages.each_with_object({}) do |lang, missing|
    get_missing_translations_for_language(keys, queries, lang).each do |file, queries_for_language|
      missing[file] ||= {}
      missing[file].merge!(queries_for_language)
    end
  end
end

#has_translation?(keys, lang, query) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/missing_t.rb', line 119

def has_translation?(keys, lang, query)
  i18n_label(lang, query).split('.').each do |segment|
    return false unless segment =~ /#\{.*\}/ or (keys.respond_to?(:key?) and keys.key?(segment))
    keys = keys[segment]
  end
  true
end

#runObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/missing_t.rb', line 67

def run
  missing = {}
  collect_missing.each do |file, message_strings|
    message_strings.each do |message_string, value|
      missing.deep_safe_merge! hashify(message_string.split('.'), value)
    end
  end

  missing.each do |language, missing_for_language|
    puts
    puts "#{language}:"
    print_hash(missing_for_language, 1)
  end
end

#translation_keysObject



99
100
101
102
103
104
105
106
107
# File 'lib/missing_t.rb', line 99

def translation_keys
  locales_pathes = ["config/locales/**/*.yml", "vendor/plugins/**/config/locales/**/*yml", "vendor/plugins/**/locale/**/*yml"]
  locales_pathes.each_with_object({}) do |path, translations|
    Dir.glob(path) do |file|
      t = open(file) { |f| YAML.load(f.read) }
      translations.deep_safe_merge!(t)
    end
  end
end

#translation_queriesObject



109
110
111
112
113
114
115
116
117
# File 'lib/missing_t.rb', line 109

def translation_queries
  files_with_i18n_queries.each_with_object({}) do |file, queries|
    queries_in_file = extract_i18n_queries(file)
    if queries_in_file.any?
      queries[file] = queries_in_file
    end
  end
  #TODO: remove duplicate queries across files
end