voltron-translate 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8db4e5fc35eb77c846ae4e2ef94e3744a2914b9b
4
- data.tar.gz: b497648aaa3bfd6ca8fbdf8b8b0e4a75316b50da
3
+ metadata.gz: 1658719544a250f6aae2906d090e405a7614db52
4
+ data.tar.gz: 1a44aea7a63899dc077fa3871a6334e6e528aa3b
5
5
  SHA512:
6
- metadata.gz: 3167d32b07bf56cec33c9533c9d569039a2f3abc8d0c154a943032ba9037402582a6c553c24863142d4a8039df683c1a571f58cfe5f91751df5494b115d0f53d
7
- data.tar.gz: 02bd0d4fc342882f2fbbeef36086602a81a270d9a7e826ff42f61a7c9f91dade7b20b4c5f09c715b81f731ee981867af65bee3f19cce7fb3e399c3b8cc09707c
6
+ metadata.gz: 6d48356fb02213c0d4d41e53cf2c6fc23c0bbba719a81252dadbc50e0acdcb89e8fe239d12f98d5bf77c2b44f3cf165c4ae1d6c640ef8334c4c78dc59f47f229
7
+ data.tar.gz: 5ceea071fb55ec9ccfc5b5fbda61d5c9c3966ff5fc0e95938c32c62085ce53cc31bb266eb6b99bdf50b742d347c3c35ad651ab9aeec1807f8ca074046c72ca4d
@@ -3,15 +3,15 @@ module Voltron
3
3
  module Generators
4
4
  class InstallGenerator < Rails::Generators::Base
5
5
 
6
- desc "Add Voltron Translate initializer"
6
+ desc 'Add Voltron Translate initializer'
7
7
 
8
8
  def inject_initializer
9
9
 
10
- voltron_initialzer_path = Rails.root.join("config", "initializers", "voltron.rb")
10
+ voltron_initialzer_path = Rails.root.join('config', 'initializers', 'voltron.rb')
11
11
 
12
12
  unless File.exist? voltron_initialzer_path
13
13
  unless system("cd #{Rails.root.to_s} && rails generate voltron:install")
14
- puts "Voltron initializer does not exist. Please ensure you have the 'voltron' gem installed and run `rails g voltron:install` to create it"
14
+ puts 'Voltron initializer does not exist. Please ensure you have the \'voltron\' gem installed and run `rails g voltron:install` to create it'
15
15
  return false
16
16
  end
17
17
  end
@@ -1,9 +1,9 @@
1
- require "voltron"
1
+ require 'voltron'
2
2
 
3
3
  module Voltron
4
4
  module Translate
5
5
  class Railtie < Rails::Railtie
6
- initializer "voltron.translate.initialize" do
6
+ initializer 'voltron.translate.initialize' do
7
7
  ::ActionController::Base.send :include, ::Voltron::Translate
8
8
  ::ActiveRecord::Base.send :include, ::Voltron::Translate
9
9
  ::ActiveRecord::Base.send :extend, ::Voltron::Translate
@@ -1,5 +1,5 @@
1
1
  module Voltron
2
2
  module Translate
3
- VERSION = "0.1.5".freeze
3
+ VERSION = '0.1.6'.freeze
4
4
  end
5
5
  end
@@ -1,9 +1,9 @@
1
- require "voltron"
2
- require "voltron/translate/version"
3
- require "voltron/config/translate"
4
- require "digest"
5
- require "csv"
6
- require "google_hash"
1
+ require 'voltron'
2
+ require 'voltron/translate/version'
3
+ require 'voltron/config/translate'
4
+ require 'digest'
5
+ require 'csv'
6
+ require 'google_hash'
7
7
 
8
8
  module Voltron
9
9
  module Translate
@@ -12,7 +12,7 @@ module Voltron
12
12
  return (text % args) unless Voltron.config.translate.enabled?
13
13
 
14
14
  begin
15
- raise "Locale can only contain the characters A-Z, and _" unless locale.to_s =~ /^[A-Z_]+$/i
15
+ raise 'Locale can only contain the characters A-Z, and _' unless locale.to_s =~ /^[A-Z_]+$/i
16
16
 
17
17
  # If app is running in one of the environments where translations can be created
18
18
  if Voltron.config.translate.buildable?
@@ -23,7 +23,7 @@ module Voltron
23
23
  translator(locale).translate text, **args
24
24
  rescue => e
25
25
  # If any errors occurred, log the error and just return the default interpolated text
26
- Voltron.log e.message.to_s + " (Original Translation Text: #{text})", "Translate", :light_red
26
+ Voltron.log e.message.to_s + " (Original Translation Text: #{text})", 'Translate', :light_red
27
27
  text % args
28
28
  end
29
29
  end
@@ -31,76 +31,116 @@ module Voltron
31
31
  def translator(locale = I18n.locale)
32
32
  @translators ||= {}
33
33
  @translators[locale.to_s] ||= Translation.new(locale)
34
- @translators[locale.to_s]
35
34
  end
36
35
 
37
36
  class Translation
38
37
 
39
- attr_accessor :locale, :list, :last_modified
38
+ attr_accessor :locale
40
39
 
41
40
  def initialize(locale)
42
41
  @locale = locale.to_s
43
- load_list # Load the list of translations into a dense google hash object for retrieval
44
42
  end
45
43
 
46
44
  def translate(text, **args)
47
45
  phrase(text, args) % args
48
46
  end
49
47
 
50
- def []=(text, translation)
51
- unless list.has_key?(text)
52
- CSV.open(path, "a", force_quotes: true) { |f| f.puts [text, translation] }
53
- list[text] = translation
54
- end
55
- end
56
-
57
48
  def destroy
58
49
  File.unlink(path) if File.exists?(path)
59
- load_list
50
+ reload
60
51
  end
61
52
 
62
53
  def path
63
- @path ||= Rails.root.join("config", "locales", "#{locale}.csv")
54
+ @path ||= Rails.root.join('config', 'locales', "#{locale}.csv")
64
55
  end
65
56
 
66
57
  def write(text)
67
- unless list.has_key?(text)
68
- CSV.open(path, "a", force_quotes: true) { |f| f.puts [text, text] }
69
- list[text] = text
70
- Voltron.log "Added translation for text '#{text}' (Locale: #{locale})", "Translate", :light_blue
58
+ # If the full list of translations does not contain the given text content, add it
59
+ unless full_list.has_key?(text)
60
+ CSV.open(path, 'a', force_quotes: true) { |f| f.puts [text, text] }
61
+ Voltron.log "Added translation for text '#{text}' (Locale: #{locale})", 'Translate', :light_blue
62
+ end
63
+ end
64
+
65
+ # Almost the same as full_list, but does not include translations whose from/to are identical,
66
+ # since that would just be unnecessarily inflating the size of the hash
67
+ def list
68
+ reload if has_been_modified?
69
+ @@list ||= begin
70
+ hsh = ::GoogleHashDenseRubyToRuby.new
71
+ data.each { |k,v| hsh[k] = v unless k == v }
72
+ hsh
71
73
  end
72
74
  end
73
75
 
76
+ # A google hash object representing the translation file, where the key is the original text,
77
+ # and the value is the translated text
78
+ def full_list
79
+ reload if has_been_modified?
80
+ @@full_list ||= begin
81
+ hsh = ::GoogleHashDenseRubyToRuby.new
82
+ data.each { |k,v| hsh[k] = v }
83
+ hsh
84
+ end
85
+ end
86
+
87
+ # The last time (in microseconds) the translation file was updated
88
+ def last_modified
89
+ Rails.cache.fetch("translations/data/modified/#{locale}") { modified }
90
+ end
91
+
74
92
  private
75
93
 
76
- def load_list
77
- @list = ::GoogleHashDenseRubyToRuby.new
78
- @last_modified = modified
79
- Rails.cache.delete_matched("translations/phrase") if Rails.cache.respond_to?(:delete_matched)
80
- CSV.foreach(path, force_quotes: true) { |row| list[row.first] = row.last unless list.has_key?(row.first) } if File.exists?(path)
94
+ # The hash representation of the csv contents. Cannot be a google hash since google hash
95
+ # objects cannot be stored in the cache. This data is reloaded anytime the csv file is modified
96
+ def data
97
+ Rails.cache.fetch("translations/data/#{locale}/#{modified}") do
98
+ hsh = {}
99
+ if File.exists?(path)
100
+ CSV.foreach(path, force_quotes: true) do |row|
101
+ hsh[row[0]] = row[1]
102
+ end
103
+ end
104
+ hsh
105
+ end
81
106
  end
82
107
 
83
108
  def phrase(text, args)
84
- load_list if modified > last_modified
85
- write(text) if Voltron.config.translate.buildable?
86
109
  key = phrase_key(text, args)
87
- Rails.cache.fetch(key) { list[text] } || text
110
+ Rails.cache.fetch(key) { list[text] || text }
88
111
  end
89
112
 
90
113
  def modified
91
114
  if File.exists? path
92
- File.mtime(path).to_i
115
+ File.mtime(path).strftime('%s%6N')
93
116
  else
94
- Time.now.to_i
117
+ Time.now.strftime('%s%6N')
95
118
  end
96
119
  end
97
120
 
121
+ # Reset the list and full list objects, so they are forced to load the new data
122
+ def reload
123
+ @@list = nil
124
+ @@full_list = nil
125
+ end
126
+
127
+ def has_been_modified?
128
+ is_modified = last_modified != modified
129
+
130
+ if is_modified
131
+ # Update last modified timestamp
132
+ Rails.cache.write("translations/data/modified/#{locale}", modified)
133
+ end
134
+
135
+ is_modified
136
+ end
137
+
98
138
  def phrase_key(text, args)
99
- key = Digest::SHA256.hexdigest [text, locale, modified].map(&:to_s).join + args.map { |k,v| "#{k}#{v}" }.join
100
- "translations/phrase/#{key}"
139
+ key = Digest::SHA256.hexdigest text + args.map { |k,v| "#{k}#{v}" }.join
140
+ "translations/phrase/#{key}/#{locale}/#{modified}"
101
141
  end
102
142
  end
103
143
  end
104
144
  end
105
145
 
106
- require "voltron/translate/railtie" if defined?(Rails)
146
+ require 'voltron/translate/railtie' if defined?(Rails)
@@ -18,14 +18,14 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "rails", ">= 4.2"
22
- spec.add_dependency "voltron", ">= 0.2.0"
23
- spec.add_dependency "google_hash", ">= 0.9.0"
21
+ spec.add_dependency 'rails', '>= 4.2'
22
+ spec.add_dependency 'voltron', '>= 0.2.0'
23
+ spec.add_dependency 'google_hash', '>= 0.9.0'
24
24
 
25
- spec.add_development_dependency "bundler", ">= 1.12"
26
- spec.add_development_dependency "rake", ">= 10.0"
27
- spec.add_development_dependency "rspec", ">= 3.0"
28
- spec.add_development_dependency "rspec-rails", ">= 3.4"
29
- spec.add_development_dependency "sqlite3", ">= 1.2"
30
- spec.add_development_dependency "simplecov", "0.11.0"
25
+ spec.add_development_dependency 'bundler', '>= 1.12'
26
+ spec.add_development_dependency 'rake', '>= 10.0'
27
+ spec.add_development_dependency 'rspec', '>= 3.0'
28
+ spec.add_development_dependency 'rspec-rails', '>= 3.4'
29
+ spec.add_development_dependency 'sqlite3', '>= 1.2'
30
+ spec.add_development_dependency 'simplecov', '0.11.0'
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voltron-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Hainer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-13 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  version: '0'
180
180
  requirements: []
181
181
  rubyforge_project:
182
- rubygems_version: 2.4.8
182
+ rubygems_version: 2.6.6
183
183
  signing_key:
184
184
  specification_version: 4
185
185
  summary: Implements double underscore method for translating and interpolating phrases