class Voltron::Translate::Translator
Attributes
locale[RW]
Public Class Methods
new(locale)
click to toggle source
# File lib/voltron/translate.rb, line 46 def initialize(locale) @locale = locale.to_s end
Public Instance Methods
destroy()
click to toggle source
# File lib/voltron/translate.rb, line 54 def destroy File.unlink(path) if File.exists?(path) reload end
full_list()
click to toggle source
A google hash object representing the translation file, where the key is the original text, and the value is the translated text
# 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()
click to toggle source
The last time (in microseconds) the translation file was updated
# File lib/voltron/translate.rb, line 94 def last_modified Rails.cache.fetch("translations/data/modified/#{locale}") { modified } end
list()
click to toggle source
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
# 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()
click to toggle source
# File lib/voltron/translate.rb, line 59 def path @path ||= Rails.root.join('config', 'locales', "#{locale}.csv") end
translate(text, **args)
click to toggle source
# File lib/voltron/translate.rb, line 50 def translate(text, **args) phrase(text, args) % args end
write(text)
click to toggle source
# 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
Private Instance Methods
data()
click to toggle source
The hash representation of the csv contents. Cannot be a google hash since google hash objects cannot be stored in the cache. This data is reloaded anytime the csv file is modified
# File lib/voltron/translate.rb, line 102 def data Rails.cache.fetch("translations/data/#{locale}/#{modified}") do hsh = {} if File.exists?(path) CSV.foreach(path, force_quotes: true) do |row| hsh[row[0]] = row[1] end end hsh end end
has_been_modified?()
click to toggle source
# File lib/voltron/translate.rb, line 133 def has_been_modified? is_modified = last_modified != modified if is_modified # Update last modified timestamp Rails.cache.write("translations/data/modified/#{locale}", modified) end is_modified end
modified()
click to toggle source
# File lib/voltron/translate.rb, line 119 def modified if File.exists? path File.mtime(path).strftime('%s%6N') else Time.now.strftime('%s%6N') end end
phrase(text, args)
click to toggle source
# File lib/voltron/translate.rb, line 114 def phrase(text, args) key = phrase_key(text, args) Rails.cache.fetch(key) { list[text] || text } end
phrase_key(text, args)
click to toggle source
# File lib/voltron/translate.rb, line 144 def phrase_key(text, args) key = XXhash.xxh32(text + args.map { |k,v| "#{k}#{v}" }.join) "translations/phrase/#{key}/#{locale}/#{modified}" end
reload()
click to toggle source
Reset the list and full list objects, so they are forced to load the new data
# File lib/voltron/translate.rb, line 128 def reload @@list = nil @@full_list = nil end