Skip to content

Commit

Permalink
Refactored the middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Nov 17, 2011
1 parent 8cb22f5 commit 21c6603
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 12 additions & 8 deletions lib/i18n-js/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@ def cache
end
end

# Check if translations should be regenerated.
# ONLY REGENERATE when these conditions are met:
#
# # Cache file doesn't exist
# # Translations and cache size are different (files were removed/added)
# # Translation file has been updated
#
def verify_locale_files!
valid_cache = []
changed_files = []
new_cache = {}

valid_cache.push cache_path.exist?
valid_cache.push ::I18n.load_path.uniq.size == cache.size

::I18n.load_path.each do |path|
change = File.mtime(path).to_i
file_has_changed = change != cache[path]
valid_cache.push file_has_changed
valid_cache.push !file_has_changed
changed_files << path if file_has_changed
cache[path] = change
changed_at = File.mtime(path).to_i
valid_cache.push changed_at == cache[path]
new_cache[path] = changed_at
end

unless valid_cache.all?
File.open(cache_path, "w+") do |file|
file << cache.to_yaml
file << new_cache.to_yaml
end

SimplesIdeias::I18n.export!
Expand Down
3 changes: 1 addition & 2 deletions spec/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require "spec_helper"

if File.basename(Rails.root) != "tmp"
warn <<-TXT
abort <<-TXT
\e[31;5m
WARNING: That will remove your project!
Please go to #{File.expand_path(File.dirname(__FILE__) + "/..")} and run `rake spec`\e[0m
TXT
exit 1
end

describe SimplesIdeias::I18n do
Expand Down

0 comments on commit 21c6603

Please sign in to comment.