Class: Voltron::Translate::Translator
- Inherits:
-
Object
- Object
- Voltron::Translate::Translator
- Defined in:
- lib/voltron/translate.rb
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#full_list ⇒ Object
A google hash object representing the translation file, where the key is the original text, and the value is the translated text.
-
#initialize(locale) ⇒ Translator
constructor
A new instance of Translator.
-
#last_modified ⇒ Object
The last time (in microseconds) the translation file was updated.
-
#list ⇒ Object
Almost the same as full_list, but does not include translations whose from/to are identical, since that would just be unnecessarily inflating the size of the hash.
- #path ⇒ Object
- #translate(text, **args) ⇒ Object
- #write(text) ⇒ Object
Constructor Details
#initialize(locale) ⇒ Translator
Returns a new instance of Translator.
46 47 48 |
# File 'lib/voltron/translate.rb', line 46 def initialize(locale) @locale = locale.to_s end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
44 45 46 |
# File 'lib/voltron/translate.rb', line 44 def locale @locale end |
Instance Method Details
#destroy ⇒ Object
54 55 56 57 |
# File 'lib/voltron/translate.rb', line 54 def destroy File.unlink(path) if File.exists?(path) reload end |
#full_list ⇒ Object
A google hash object representing the translation file, where the key is the original text, and the value is the translated text
84 85 86 87 88 89 90 91 |
# File 'lib/voltron/translate.rb', line 84 def full_list reload if has_been_modified? @@full_list ||= begin hsh = ::GoogleHashDenseRubyToRuby.new data.each { |k,v| hsh[k] = v } hsh end end |
#last_modified ⇒ Object
The last time (in microseconds) the translation file was updated
94 95 96 |
# File 'lib/voltron/translate.rb', line 94 def last_modified Rails.cache.fetch("translations/data/modified/#{locale}") { modified } end |
#list ⇒ Object
Almost the same as full_list, but does not include translations whose from/to are identical, since that would just be unnecessarily inflating the size of the hash
73 74 75 76 77 78 79 80 |
# File 'lib/voltron/translate.rb', line 73 def list reload if has_been_modified? @@list ||= begin hsh = ::GoogleHashDenseRubyToRuby.new data.each { |k,v| hsh[k] = v unless k == v } hsh end end |
#path ⇒ Object
59 60 61 |
# File 'lib/voltron/translate.rb', line 59 def path @path ||= Rails.root.join('config', 'locales', "#{locale}.csv") end |
#translate(text, **args) ⇒ Object
50 51 52 |
# File 'lib/voltron/translate.rb', line 50 def translate(text, **args) phrase(text, args) % args end |
#write(text) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/voltron/translate.rb', line 63 def write(text) # If the full list of translations does not contain the given text content, add it unless full_list.has_key?(text) CSV.open(path, 'a', force_quotes: true) { |f| f.puts [text, text] } Voltron.log "Added translation for text '#{text}' (Locale: #{locale})", 'Translate', ::Voltron::Translate::LOG_COLOR end end |