Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
51  lib/translator.rb
@@ -56,46 +56,55 @@ def self.choose_locale(locale_choice)
56 56
     @current_locale = locales.detect{|l| l == locale_choice.to_sym }
57 57
   end
58 58
 
59  
-  def self.stored_keys_filepath
  59
+  def self.cached_keys_filepath
60 60
     FileUtils.mkdir_p File.join(@cache_path, current_locale.to_s)
61  
-    @stored_keys_filepath = File.join(@cache_path, current_locale.to_s, 'stored_keys.json')
  61
+    @cached_keys_filepath = File.join(@cache_path, current_locale.to_s, 'cached_keys.json')
62 62
   end
63 63
 
64  
-  def self.has_stored_keys?
65  
-    File.exists?(Translator.stored_keys_filepath)
  64
+  def self.locale_keys
  65
+    Translator.current_store.keys("#{Translator.current_locale.to_s}.*")
  66
+  end
  67
+
  68
+  def self.locale_cache_key
  69
+    "i18n.cache.#{Translator.current_locale}.keys"
  70
+  end
  71
+
  72
+  def self.cached_keys
  73
+    Translator.current_store.smembers(Translator.locale_cache_key)
  74
+  end
  75
+
  76
+  def self.cache_keys!
  77
+    Translator.current_store.del(Translator.locale_cache_key)
  78
+    keys_to_cache = Translator.locale_keys.flatten.uniq
  79
+    Rails.logger.debug "Found #{keys_to_cache.size} cache keys"
  80
+    Translator.current_store.multi do
  81
+      keys_to_cache.map{|k| Translator.current_store.sadd(Translator.locale_cache_key, k) }
  82
+    end
66 83
   end
67 84
 
68 85
   def self.store_keys_to_file!
69 86
     # Load all the keys for the current locale, stripping off the locale part
70  
-    locale_keys = Translator.current_store.keys("#{Translator.current_locale.to_s}.*").map do |k|
  87
+    Translator.locale_keys.map do |k|
71 88
       k.sub(/^#{Translator.current_locale.to_s}\./, '')
72 89
     end.uniq
73  
-    File.open(Translator.stored_keys_filepath, 'w') do |f|
  90
+    File.open(Translator.cached_keys_filepath, 'w') do |f|
74 91
       f.puts ActiveSupport::JSON.encode(locale_keys)
75 92
     end
76 93
   end
77 94
 
78  
-  def self.stored_keys(force = false)
79  
-    return @stored_keys if !force && Translator.has_stored_keys? && @stored_keys
80  
-
81  
-    Rails.logger.debug "falling through to stored_keys method"
82  
-    # Passing true into this method forces a cache file rewrite for the current locale.
83  
-    Translator.store_keys_to_file! if force
84  
-    
85  
-    # Check again if the cache file exists...
86  
-    if Translator.has_stored_keys?
87  
-      Rails.logger.debug "populating @stored_keys from cached json"
  95
+  def self.load_cached_keys_from_file(force = false)
  96
+    # Check if the cache file exists...
  97
+    if File.exists?(Translator.cached_keys_filepath)
88 98
       # decode the cached json
89  
-      @stored_keys = ActiveSupport::JSON.decode(File.read(Translator.stored_keys_filepath))
  99
+      @cached_keys = ActiveSupport::JSON.decode(File.read(Translator.cached_keys_filepath))
90 100
     end
91  
-    Rails.logger.debug "stored keys size: #{@stored_keys.size}"
92  
-    @stored_keys || []
  101
+    @cached_keys || []
93 102
   end
94 103
 
95 104
   def self.keys_for_strings(options = {})
96 105
     @simple_backend.available_locales
97 106
 
98  
-    keys = Translator.stored_keys
  107
+    keys = Translator.cached_keys
99 108
 
100 109
     if options[:filter]
101 110
       keys = keys.select {|k| k[0, options[:filter].size] == options[:filter]}
@@ -103,7 +112,7 @@ def self.keys_for_strings(options = {})
103 112
 
104 113
     case options[:group].to_s
105 114
     when "framework"
106  
-      keys.select! {|k| @framework_keys.include?(k) }
  115
+      keys = keys.select {|k| @framework_keys.include?(k) }
107 116
     when "application"
108 117
       keys -= @framework_keys
109 118
     end
12  translator.gemspec
@@ -2,13 +2,13 @@
2 2
 
3 3
 Gem::Specification.new do |s|
4 4
   s.name        = "translator"
5  
-  s.version     = "0.0.1"
  5
+  s.version     = "0.2.0"
6 6
   s.platform    = Gem::Platform::RUBY
7  
-  s.authors     = ["Hubert Łępicki"]
8  
-  s.email       = ["hubert.lepicki@amberbit.com"]
9  
-  s.homepage    = "http://github.com/amberbit/translator"
10  
-  s.summary     = "Rails engine to manage translations"
11  
-  s.description = "translator is engine, that you can easily integrate with your administration panel, and let your clients do the dirty work translating the site"
  7
+  s.authors     = ["Jacqui Maher", "Hubert Lepicki"]
  8
+  s.email       = ["jacqui.maher@nytimes.com", "hubert.lepicki@amberbit.com"]
  9
+  s.homepage    = "http://github.com/jacqui/translator"
  10
+  s.summary     = "Rails engine to manage translations, modified for our projects"
  11
+  s.description = "translator is an engine that easily integrates with your rails app"
12 12
 
13 13
   s.required_rubygems_version = ">= 1.3.6"
14 14
 

No commit comments for this range

Something went wrong with that request. Please try again.